Introduction to Java Programming
Session on Primitive Data Types In Java
Session on use of printf() method in Java
Download Java:
Link for Downloading JDK is given below
JDK DownloadDownload Eclipse
Link for Downloading Eclipse is given below
Eclipse 2024-06 DownloadNote: For any later versions of Eclipse JDK 11 or a newer version is needed
Data Types in Java
Data Type | Syntax | Size (bits) | Range | Default Value |
---|---|---|---|---|
byte | byte myByte; | 8 | -128 to 127 | 0 |
short | short myShort; | 16 | -32,768 to 32,767 | 0 |
int | int myInt; | 32 | -2,147,483,648 to 2,147,483,647 | 0 |
long | long myLong; | 64 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0L |
float | float myFloat; | 32 | Approximately ±3.40282347E+38 | 0.0f |
double | double myDouble; | 64 | Approximately ±1.7976931348623157E+308 | 0.0d |
char | char myChar; | 16 | Unicode characters | '\u0000' |
boolean | boolean myBoolean; | 1 bit | true or false | false |
Declaring a variable with int data type in Java
public class javalabclass{
public static void main(String args[])
{
//Declaring an int Variable
int a =2147483647;
System.out.println(a);
}
}
Declaring a variable with long data type in Java
public class javalabclass{
public static void main(String args[])
{
//Declaring a Long Variable
long a =2147483648L;
System.out.println(a);
}
}
Declaring a variable with short data type in Java
package javaprogrammingdemo;
public class javalabclass{
public static void main(String args[])
{
//Declaring an short Variable
short a =32767;
System.out.println(a);
}
}
Declaring a variable with float data type in Java
package javaprogrammingdemo;
public class javalabclass{
public static void main(String args[])
{
//Declaring an float Variable
float k = 2.3f;
System.out.println(k);
}
}
Declaring a variable with double data type in Java
package javaprogrammingdemo;
public class javalabclass{
public static void main(String args[])
{
//Declaring an double Variable
double h = 2.34;
System.out.println(h);
}
}
Declaring a variable with char data type in Java
package javaprogrammingdemo;
public class javalabclass{
public static void main(String args[])
{
//Declaring an char Variable
char h = 'A';
System.out.println(h);
}
}
Declaring a variable with boolean data type in Java
public class javalabclass{
public static void main(String args[])
{
//Declaring an char Variable
boolean a = true;
System.out.println(a);
}
}
Exercise - Write a program to store and print the variables of the following data Types
int
long
short
byte
float
double
boolean
package javaprogrammingdemo;
public class javalabclass{
public static void main(String args[])
{
int a=2;
float b=23.f;
long k = 23567L;
short m = 3;
double j = 234.556;
boolean choice = true;
byte h = 12;
System.out.println(a);
System.out.println(b);
System.out.println(k);
System.out.println(m);
System.out.println(j);
System.out.println(choice);
System.out.println(h);
}
}
Output
2
23.0
23567
3
234.556
true
12
Print Hello World Message to the user
public class javalabclass{
public static void main(String args[])
{
System.out.println("hello World");
}
}
Print an integer value with a message to the user
public class javalabclass{
public static void main(String args[])
{
int a=4;
System.out.println("The marks you have scored is "+a);
}
}
Print the message Satish you have scored 4 Marks using printf function
public class javalabclass{
public static void main(String args[])
{
int a=4;
String s = "Satish";
System.out.printf("%s You have scored %d marks ",s,a);
}
}
Print a character within a String using printf function
public class javalabclass{
public static void main(String args[])
{
char t = 'A';
System.out.printf("The first character is %c",t);
}
}
output - The first character is A
Code for breaking a sentence using newline character
public class javalabclass{
public static void main(String args[])
{
System.out.printf("Satish %nshould never %nbe a professor");
}
}
output
Satish
should never
be a professor
Formatting float output to have a certain number of decimal places
//printing only 2 decimal places in the output
public class javalabclass{
public static void main(String args[])
{
float k = 3.4578f;
System.out.printf("%.2f",k);
}
}