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


3. Write a Java Program to demonstrate type casting in using primitive data
types

intbyte,short
float,double byte,short,int,long

int  char
int i =95;
char c = i;
System.out.println(i);

4. Write a Java Program to demonstrate type promotion in expressions using


primitive data types
(1)
byte s=10;

byte p = 20;

byte result = s+p;


System.out.println(result);

(2)
short s=10;
short p = 20;
short result = s+p;
System.out.println(result);

(3) byte b=10;


byte result = b*2;
System.out.println(result);

(4)int a =10;
int b=3;
int result = a/b;
System.out.println(result);

N Nagarjuna OOP through Java


5. Write a Java Program to construct the following pyramids

N Nagarjuna OOP through Java

You might also like