More on Data Types


Collapse Content

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.

This is because 5 and 2 are assumed to be integers, and during division any decimal value is lost. To keep decimal values, you need to use floating-point types. There are 2 types for holding "decimal", or real numbers, like 2.25 and 0.003:

  • float - this is a 32-bit type for storing real numbers which can be useful for simple values. It will become imprecise when used in operations with very large or very small values. To create a float value, you need to add an f after the number. For example, float f = 1.23f;
  • double - this is a 64-bit type for storing real numbers, which should generally be used. A number with a decimal value (like 1.23) is assumed to be a double in Java.

characters

The char type is is used in Java for storing a single character of text. A char is a single character enclosed in single quotes. For example:

char c = 'x';
char d = '5';

A char can also store many values that do not appear on the keyboard. These are specified using special sequences that are 'escaped' with the \. The most common one is the newline character: '\n', which is used to create a newline (usually in Strings). To store words of text, you use the String type, which uses characters internally for storage.

chars are really numbers
The char in Java is a 16-bit type for representing any unicode character. The char is stored as an integer in the computer with a range from 0 to 65,535. Since char is stored as an integer, it can be treated as one too. For example, you can add to a char:

char c1 = 'a';
char c2 = 'a' + 1;
System.out.println("c2 is " + c2);

This will print:

c2 is b

This is because b is represented by an integer 1 more than a (specifically 98 and 97). In fact, all the alphanumeric characters are represented by integers in order, so you could always add 1 to a letter to get the next letter.

Since characters can be integers, you need to watch out for the following error:

System.out.println(c1 + c2);

You might expect this to print ab like it would for Strings a and b. However, since chars are integers, Java will add their values together and print 195 instead. + is used to concatenate Strings, but when dealing with numbers it will add them.

Challenge

Given a character from A to U, print the character and the next 5 characters in the alphabet (space-separated). Print the characters in the same case as the given one.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Challenge

Create a float called cent with the value 0.01 in one line of code.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

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]