0% found this document useful (0 votes)
31 views

Helloworld - Java. Compiling Java Program at Console or Terminal

This document provides examples of Java programs to demonstrate various concepts: 1) A "Hello World" program that prints the message to the console. 2) A program showing type promotion through assigning smaller data types like byte to larger types like long. 3) A program demonstrating type casting between primitive data types like int to byte using both implicit and explicit casting. 4) A program exhibiting type promotion in expressions, such as adding two bytes which are promoted to int before calculation. 5) An example to write a program constructing number pyramids through nested for loops.

Uploaded by

Shiva Ram
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Helloworld - Java. Compiling Java Program at Console or Terminal

This document provides examples of Java programs to demonstrate various concepts: 1) A "Hello World" program that prints the message to the console. 2) A program showing type promotion through assigning smaller data types like byte to larger types like long. 3) A program demonstrating type casting between primitive data types like int to byte using both implicit and explicit casting. 4) A program exhibiting type promotion in expressions, such as adding two bytes which are promoted to int before calculation. 5) An example to write a program constructing number pyramids through nested for loops.

Uploaded by

Shiva Ram
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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