PRIMITIVE
DATA TYPES- INTEGER
The
usage of ‘int’ data type to store a number is discussed in the previous
section. Now, let us see how to declare an integer using data types like long, byte
and short.
For
using ‘long’ data type, the number should end with either ‘l’ or ‘L’. Each
variable should be allocated with different names, else the system will be
confused.
Java program in JDK15
class primitive{
public static void main(String
args[]){
byte number_1=11;
short number_2=1129;
int number_3=211211211;
long number_4=1129112911291129L;
long number_5=1129112911291129l;
System.out.println(number_1);
System.out.println(number_2);
System.out.println(number_3);
System.out.println(number_4);
System.out.println(number_5);
}
}
Output
Variables
should never start with a number eg) int
2var=10 results in error whereas using int
var2=10 is correct.
PRIMITIVE
DATA TYPES- FLOATING POINT
Floating
values should end with either ‘F’ or ‘f’ whereas, double does not require
ending with any alphabets.
Java program for floating point numbers
class primitive{
public static void main(String args[]){
float number_1=11.29F;
float number_2=11f;
double number_3=21121.1129;
double number_4=112911;
System.out.println(number_1);
System.out.println(number_2);
System.out.println(number_3);
System.out.println(number_4);
}
}
Output
In number 2, I had not defined any numbers after the
decimal. Hence, it is assigned as 0. The same case occurs in double data type.
NON-NUMERIC
PRIMITIVE DATA TYPE
‘Char’ data type should be always declared within
single codes else it results in error. The keywords are case sensitive and
hence must be used properly to avoid errors. ‘System.out.print’ outputs the variables
in a same line whereas, ‘System.out.println’ outputs the elements in the next
line. The shortcut for using ‘System.out.println’ is sout+Tab and this keyword
is useful since this line is printed often in Java. To type strings, double
codes are used.
Java program to print non-primitive data type
class primitive{
public static void main(String args[]){
char a_1='x';
char a_2='o';
char a_3='x';
char a_4='o';
boolean a_5=true;
boolean a_6=false;
System.out.print(a_1);
System.out.print(a_2);
System.out.print(a_3);
System.out.println(a_4);
System.out.println(a_5);
System.out.println(a_6);
}
}
Output
Boolean command is used for displaying only true or
false and it is extensively used in many applications.
Executing code in Online compiler
I have used this link to execute the Java code: ONLINE COMPILER
You can also try using this or any other online Java
complier.
Output







1 Comments
Nice
ReplyDelete