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:
byte
- This can store integers from -127 to 127. You may have guessed that they take up one byte of memory, or 8 bits. It is sometimes used when dealing with raw data or in special low-memory circumstances.short
- This is a 16-bit type that can store integers from –32,768 to 32,767. It is almost never used.
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 abyte
orshort
instead of anint
for small integer values. When you write an integer literal like3
or1234
in Java, it is assumed to be of typeint
.long
- Theint
can go from about -2 Billion to +2 Billion, but sometimes you need to deal with longer numbers. This is wherelong
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 along
you put aL
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.
Learneroo
Dec 11, 5:24 PM@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+" ");
Mandy
Oct 27, 2:54 PMwhy is this incorrect?
class Main{
static void doStuff(char c){
for (char c=a; c<=21; c=a+1) {
System.out.println(c+"");
}
}
thales
Jul 10, 5:49 AMThey should of have explained the difference between i=i+1 and i++, cause in this case this is not the same!
thales
Jul 10, 5:50 AMfor(char i=c; i<=c+5; i++){
System.out.print(i+" ");
Learneroo
Jul 10, 10:34 AM@thales, they're basically always the same, but make sure you don't put
i=i++
when you meani=i+1
.Yasin A. Shuman
Aug 22, 12:20 AMI dont understand why it is giving me a compilation error:
here is my code:https://www.learneroo.com/user_answers/2687389458
Learneroo
Aug 22, 8:36 AMI think you meant to use
int i
instead ofchar i
.Yasin A. Shuman
Aug 22, 6:31 PMchanging 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
catypus
Jun 11, 1:00 AMI just found out you have to use print instead of println... :(
Why?
catypus
Jun 11, 1:05 AMOK 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+ " ");
}
Learneroo
Jun 11, 3:48 PMYou'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)