Java Programs on Data Type, Variable and Arrays with Explanation

Java Programs on Data Type, Variable and Arrays


1. Write a program to compute distance light travel in 1000 days using long variables.

DESCRIPTION:  This program calculate the speed of light in 1000 days. We take long variable to store the speed of light because int type is not large enough to hold the desire value. The range of a long is quite large. This makes it useful when big whole numbers are needed.

class Light{         // This line declares a new class & the class definition is start.

public static void main(String a[]){       // This line begins the main() method.

int lightspeed;          //declare a integer name lightspeed.

long days;              //declare a variable days of datatype long.

long seconds, distance;

lightspeed=186000;        //approximate speed of light n miles per second.

days=1000;                //specify number of days.

seconds=days*24*60*60;        //convert to seconds.

distance=lightspeed * seconds;        //compute distance

System.out.print("In " + days);

System.out.print(" Days light will travel about ");

System.out.println(distance + " miles.");     //print distance in miles.

}

}

OUTPUT:


Write a program that calculates the area of a circle, use data type of double.

CODE:

class Area{ // This line declares a new class & the class definition is start.

public static void main(String a[]){ // This line begins the main() method.

double pi, r, b; // daclere three variable of type double.

r=12.3993; //radius of circle.

pi=3.1416; // value of pi

b=pi*r*r; // Declare area formula.

System.out.println("Area of the circle is " + b); //This print the area of circle.
}
}

DESCRIPTION: In this program first declare a class name Area and show main program and than I declare pi r and b and declare value of r and pi after we declare how area found after this in system.out.println line it gives us the value of area

OUTPUT:

Java Programs on Data Type, Variable and Arrays


Write a program which demonstrate the char data type.

class CharDemo{ // Declare class name as CharDemo.

public static void main(String a[]){ // This line begins the main() method.

char ch1, ch2; // daclere two character variable.

ch1=88; //Declare value of ch1

ch2='Y'; //Declare ch2 is Y

System.out.print("ch1 and ch2: "); //print the string.

System.out.println(ch1 + " " + ch2); //display both character.

}

}

DESCRIPTION: In this program I take two character variable ch1 assigned the value 88 which is the ASCII (and UNICODE) value that corresponds to the letter X. At the end we display both character value. The program displays the following output.

OUTPUT:

Java Programs on Data Type, Variable and Arrays


Write a program in which char variables behave like integers.

CODE:

class CharDemo2{        // This line declares a class name and a class start from here .

public static void main(String a[]){ // This line begins the main() method.

char ch1; // declare a character variable.

ch1='X'; // give a charter to ch1

System.out.println("ch1 contains " + ch1); //this line print ch1

ch1++; //increment ch1.

System.out.println("ch1 is now " + ch1); //print incremented character.

} //end of main funcation

} //end of class

DESCRIPTION: In this program I ist make a class of name Chardemo2. In second line main funcation of programme start, after this I initiliaze and declare ch1,after this we print ch1 , then increment in value of ch1 and print value of ch1

OUTPUT:

Java Programs on Data Type, Variable and Arrays


Write a program which demonstrate dynamic initialization.

class Square{                        // This line daclare a class ”Square” & the class is start from here .

public static void main(String a[]){                              // This line begins the main() method.

double x=3.0, y=4.0;                             // Initiallize two double variables.

double z=Math.sqrt(x*x + y*y);                             //z is initialized and declare calue of z

System.out.println("Hypotenuse is " + c);                                  //this line print value of Hypotenus

}                                                // end main funcation

} end of class name

Description: this program computes the length of the hypotenuse of right triangle given the length of two opposing sides.

OUTPUT:


Write a program that demonstrates the Boolean type values.

CODE:

class BoolTest{                                   // This line daclare a new class & the class defination is start.

public static void main(String a[]){                               // This line begins the main() method.

boolean b;                                                // daclere a boolean variable.

b=false; //Initialize b is false.

System.out.println("b is " + b);                                 // display b .

b=true; //now b initialize is true.

System.out.println("b is " + b);                                     // display it.

if(b)

System.out.println("this is executed. ");

// if condition is true then this string is print

b=false; // b again false.

if(b) // initialize if condition

System.out.println("this is not executed. ");

//this line is not display because b is false.

System.out.println("10 > 9 is " + (10>9)); //out come relational operator Boolean which is true

} //end of main function

} // end of a class

DESCRIPTION: I

OUTPUT:


Write a program which show the effect of nested scopes.

CODE:

class Scope{ // This line daclare a new class & the class defination is start.

public static void main(String a[]){ // This line begins the main() method.

int x; // initialize x

x=10; //Declare value of x.

if(x==10){ //start new scope

int y=20; //this y is known only to this block.

System.out.println("x and y : " + x + " " + y);              // This line show the output

x=y*2; // this line show x equals to y*2

}                                  // this line show the finishing of block

          //y=100;                  // error! y not known here.

System.out.println("x is " + x);                  //this line show the output output

}                                      // this line show the end of program

} this line show the end of class

DESCRIPTION: This program shows the scope of any variable. As the comment indicate we Ist declare the class and then initialize and delare

OUTPUT:


Write a program to demonstrates the lifetime of a variable.

class LifeTime{ // This line daclare a class name

public static void main(String a[]){ // This line begins the main() method.

int x; // daclare a variable x.

for(x=0; x<3; x++){ //Declare for loop

int y=-1; //y is initialized each time block is entered.

System.out.println(“y is: ” + y); this is a output line

y=100; Give value to y

System.out.println(“y is now: ” + y); // this is the output line

} // end of loop function

} // this line show the main program

}

Description: In this program we First declare declare a class name Lifetime than main program start , I declare first x and and than declare for loop ; after this after this I initilaze y and print value of y; after again give value to y and print again value of y

OUTPUT:


Write a program that convert one data type in to another data type

class Conversion { // Declare class name

public static void main(String args[]){ //daclre main line line of program main body of program start from here

int i = 257; //Initialize and Declare i

byte b; //initialize b

double d = 323.42; // Initialize and Declare d

System.out.println(“\n Conversion of int tom byte.”); // print string line

b = (byte) i; // convertion of data type

System.out.println(” i and b is “+ i + ” ” + b); // print value of I and b

System.out.println( “\n Conversion od double to int ” ); //print string line

i = (int) d ; // convertion of data type

System.out.println(“d and i ” + i + ” ” + d ); // print value of I and d

System.out.println(“\n Conversion of double to byte”); // print string line

b = (byte) d; // convert value of b in to byte

System.out.println(“d and b ” + d + ” ” + b); print value of b and d

} // end of main program

} // end of class

Description:

In this program first make a class and write it main main method after intiliaze I, b, d. after we conert one data type in to another data type and prind their value

Output:


Write a program that multiply and add different data type variable.

class Promote { // class name start from here

public static void main (String args [ ] ) { // Main body of program start from here

byte b = 42; //Initialize and declare b

char c='a'; //Initialize and declare c

short s = 1024; //Initialize and declare s

int i = 50000; //Initialize and declare I

float f = .67f; //Initialize and declare f

double d = .1234; //Initialize and declare d

double result = (f * b) + (i / c) + (d * s); //Initialize and declare result

System.out.println((f * b) + " " + (i / c) + " "+ (d * s)); // print line

System.out.println("result = " + result); // print result

} // end of main programme

} // end of a class

Description:

In this program I first make class and write its main method after this I declare and initialize b, c, s, I, f, d. after this initialize result and give a value after we print our result

Output:


Write a program which demonstrate the arithmetic operator

class BasicMath{ // This line daclare a new class & the class defination is start.

public static void main(String a[]){ // This line begins the main() method.

System.out.println("Integer Arithmetic"); //arthmetic using integers

int x=1+1; //store 2 in variable x

int b=x * 3; //x multiply a by 3 and store in b.

int c=b/4; //b divide b by 4 and store in c.

int d=c - x; // c minus x and store in d

int e=-d; // minus of d is

System.out.println("a = " + x); // print value of a

System.out.println("b = " + b); // print value of b

System.out.println("c = " + c); // print value of c

System.out.println("d = " + d); // print value of d

System.out.println("e = " + e); // print value of e

System.out.println("\nFloating Point Arithmetic"); // Arthmetic using doubles

double da=1+1; //store 2 in variable da

double db=da * 3; //multiply da by 3 and store in db.

double dc=db/4; //divide db by 4 and store in dc.

double dd=dc - da; // value of dc minus da store in dd

double de=-dd; // value of dd store in de

System.out.println("da = " + da); // print value of da

System.out.println("db = " + db); // print value of db

System.out.println("dc = " + dc); // print value of dc

System.out.println("dd = " + dd); // print value of dd

System.out.println("de = " + de); // print value of de

} // end of main method

} // end of class

DESCRIPTION: in this programme first make a class and write its main method, after intilize and declare a, b, c, d, e. and da, db, dc, dd, and print their value .

OUTPUT:

Java Programs on Data Type, Variable and Arrays


Write a program that demonstrate modulus operator

class modulas{ // class name

public static void main(String arg []){ // main method

int x=67; // initialize and declare x

double y=67.25; //initialize and declare y

System.out.println("x mod 10 = " + x%10 ); //print modulus of x

System.out.println("y mod 10 = " + y%10 ); //print modulus of y

} // end main method

} end class

Description: in this program first make a class and write its main method than initialize and declare c and y and print them.

Output:


Write a program in which various assignment operator is used.

//Demonstrate several assignment operator

class OpEquals{ // class name

public static void main(String a[]){ // This line begins the main() method.

int i=1; // declare i

int b=2; // Declare b

int c=3; // Declare 3

i+=5; // increment 5 in i.

b*=4; //multiply b by 4 than again store in b.

c+=i*b; //I multiply with b and add in c

c%=6; // modulus of c.

System.out.println("i = " + i); // print value of i

System.out.println("b = " + b); // print value of b

System.out.println("c = " + c); // print value of c

} end main method

} // end the class

Description: in this program first make a class and write its main method initialize and declare a, b , c and again declare a, b, c and print their value

Output:

Java Programs on Data Type, Variable and Arrays


Write a program which shows the function of increment(++).

class InDec{ // Class name and class start from here.

public static void main(String a[]){ // main method of program line.

int a=1; // Initialize1 in variable a.

int b=2; // Initialize 2 in variable b.

int c, d; // daclare two variables c & d.

c=++b; // increment in b than store in c.

d=a++; // store value of a in d. than increment in i.

System.out.println("a = " + i); //this print the value i.

System.out.println("b = " + b); //this print the value b.

System.out.println("c = " + c); //this print the value c.

System.out.println("d = " + d); //this print the value d.

}

}

DESCRIPTION: In this program two variable initialized and other two variables are initialized by increment in values of pervious two variables.

OUTPUT:

Java Programs on Data Type, Variable and Arrays


Write a program left shifting the byte value.

class ByteShift {       // class name

public static void main (String args [ ] ) { // main body of programme start from here
byte a = 64, b; //Declare a

int i; // declare i
i = a<<2; //Decare i is lessthan 2
b = (byte) (a << 2); // convert data type

System.out.println("orignal value of a: " +a); //print value of a
System.out.println("i and b:" + i + " " + b); // print value of i and b

} // main of main programme

} //end of class

Description:

Output:

Java Programs on Data Type, Variable and Arrays

Write a programme that demonstrate the Boolean logical operator

class BoolLogic { // calss name

public static void main (String args[]) { // main method of programm

boolean a=true; // Declare a

boolean b=false; //Declare b

boolean c=a | b; //Declare c

boolean d=a & b; //Declare d

boolean e=a ^ b; //Declare e

boolean f=(!a & b) | (a & !b); //Declare f

boolean g= !a; //Declare g

System.out.println(" a = " + a); //Print value of a

System.out.println(" b = " + b); //Print value of b

System.out.println(" a|b = " + c); //Print value of c

System.out.println(" a&b = " + d); //Print value of d

System.out.println(" a^b = " + e); //Print value of e

System.out.println(" (!a & b) | (a & !b) = " + f); //Print value of f

System.out.println(" !a = " + g); //Print value of g

} //main body of programme ended

} // class ended

Description:

Output:


Write a program which demonstrates the ? operator.

class Ternary{ // Declare class name

public static void main(String a[]){ // Main body of programme start from here.

int i, k; // daclare two variables i & k.

i=10; // Initialize i

k= i<0 ? -i : i; //get absolute value of i.

System.out.print("Absolute value of "); // print string line

System.out.println(i +" is "+ k); //this print the value of i and k.

i=-10; // Initialize

k= i<0 ? -i: i; // get absolute value of i.

System.out.print("Absolute value of "); // print string line

System.out.println(i +" is "+ k); //this show the value of i and k.

} // Main body of programme ended

} // class ended

Description: This program shows the functionality of ? operator. In this programme ist declare two variable and initialize them than print their value after this again initialize both variable and print their value.

OUTPUT:

Java Programs on Data Type, Variable and Arrays

Leave a Reply