You are on page 1of 3

1.

Write a Java Program to display a message “Hello World” on the console

class HelloWorld {

public static void main(String args[]) {

System.out.println("Hello, World!");

}

}

Create the program by typing it into a text editor and saving it to a file –
HelloWorld.java.

Compiling Java program at console or terminal

javac HelloWorld.java

Executing(run) Java program at console or terminal

java HelloWorld

Output

Hello, World!

2. Write a Java Program to demonstrate type promotion using primitive data
types(Widening Conversion/Type Coercion/Implicit Type casting/Automatic
Type Conversion)
When you assign value of one data type to another, the two types
might not be compatible with each other. If the data types are
compatible, then Java will perform the conversion automatically known
as Automatic Type Conversion and if not then they need to be casted
or converted explicitly. For example, assigning an int value to a long
variable.
Assigning value of a smaller data type to a bigger data type.

byte shortintlong floatdouble
charint
char c =’a’;
int i=c;
System.out.println(i);

N Nagarjuna OOP through Java

out.long int  char int i =95.out. char c = i.println(result). (4)int a =10. System. (2) short s=10.short. byte result = b*2. System. N Nagarjuna OOP through Java . Write a Java Program to demonstrate type casting in using primitive data types intbyte. byte result = s+p.int. int b=3.out. short result = s+p.short float. System.println(result). (3) byte b=10. int result = a/b.println(result).println(i). Write a Java Program to demonstrate type promotion in expressions using primitive data types (1) byte s=10. System. 3. System. short p = 20. byte p = 20.println(result).out.double byte.out. 4.

Write a Java Program to construct the following pyramids N Nagarjuna OOP through Java . 5.