More on Data Types


Premium Content - Free Preview

The 4 common primitive data types were covered previously; this node will look at all the different data types in more depth.

In Java everything is an Object except the primitive types which represent actual data in memory. Java uses primitive types to keep things optimized for the computer, though it makes it a bit more confusing for the programmer.

No Object is created when assigning primitive types, so you can just assign a variable a literal value without any new keyword. For example:

int num = 5;
char ch = 'c';

Each type in Java takes up a certain amount of memory and can therefore hold a certain range of values. The size in measured in bits, i.e. a single 1 or 0. The range of an integer data type is simply 2bits. For example, an 8-bit number will be able to hold 256 values.

integer types

There are 4 integer data types in Java. byte and short are the 2 uncommon ones, which you may never need:

toggle byte and short


The next two integer types are more common:

  • int - This is a 32-bit type which can store numbers from –2,147,483,648 to 2,147,483,647. int is used for almost all integers in Java, even small ones. For example, its used as a counter in loops and to track locations in arrays.
    It is not necessary (or even helpful) to use a byte or short instead of an int for small integer values. When you write an integer literal like 3 or 1234 in Java, it is assumed to be of type int.
  • long - The int can go from about -2 Billion to +2 Billion, but sometimes you need to deal with longer numbers. This is where long is useful. It is a 64-bit type which can store numbers from - 263 to 263 (which is a 9 followed by 18 digits.)
    To mark a number as a long you put a L after it. For example:
    long num1 = 123456789000L;
    long num2 = 12L

floating-point types

The above types can only deal with integers, so any decimal numbers that come up will be rounded down to integers. For example System.out.println(5/2); will print 2 instead of 2.5.


End of Free Content Preview. Please Sign in or Sign up to buy premium content.

Comments

  • I ended up resolving the problem using a type cast, as otherwise I received errors when adding to the char.

  • @no_name, that's one way though you don't need a cast. The following code works, and c can be incremented too. System.out.print(c+" ");

  • why is this incorrect?

    class Main{
    static void doStuff(char c){
    for (char c=a; c<=21; c=a+1) {
    System.out.println(c+"");
    }
    }

  • They should of have explained the difference between i=i+1 and i++, cause in this case this is not the same!

  • for(char i=c; i<=c+5; i++){
    System.out.print(i+" ");

  • @thales, they're basically always the same, but make sure you don't put i=i++ when you mean i=i+1.

  • I dont understand why it is giving me a compilation error:
    here is my code:https://www.learneroo.com/user_answers/2687389458

  • I think you meant to use int i instead of char i.

  • changing it to int i gave me the following output :
    97 98 99 100 101 102
    81 82 83 84 85 86
    100 101 102 103 104 105
    66 67 68 69 70 71

  • I just found out you have to use print instead of println... :(
    Why?

  • OK here's a better question:

    Why does inputting Q to this give 'b'. It should output Q!

    for(char i=c;i<c+5;i++){
    System.out.println(i+ " ");
    }

  • You're supposed to print each group of characters on its own line. If you print each individual character on its own line, the solution for the first challenge will end up being printed out for the second challenge.

    (You can always click on the Raw I/O button to see the full input and output)

Contact Us
Sign in or email us at [email protected]