JAVA PROGRAMMING CS407
CHAPTER-1
INTRO TO JAVA
1. Write a java program to implement types of variables in java programming
THEORY:
VARIABLE is nothing but a container or name given to a particular memory location.
Variable declaration is very simple. Simply use the data type name and variable name. If you
want to initialize the values then use “=” and assign the values. In java we are having 3 types of
variables
1. Local variable: the variables whose scope is within a method/at particular spot
2. Instance variable: the variables which are executed under a class using objects
3. Static variable: the variables whose scope is throughout the program and there is no need of
object to initialize it
PROGRAM:
class variable
{
public static int num1=123;
public static int num2=62; //static variable
int x,y; //instance variable
void method()
{
int a=46;
int b=45; //local variables
int c=a-b;
System.out.println("local:"+c);
}
int show(int x,int y)
PAGE NAME: G. Sharanya
NO: PIN:23001-CS-061
, JAVA PROGRAMMING CS407
{
return(x+y);
}
int display(int num1,int num2)
{
return(num1-num2);
}
public static void main(String ar[])
{
variable obj=new variable();
obj.method();
System.out.println("instance:"+obj.show(61,22));
System.out.println("static:"+obj.display(num1,num2));
System.out.println("DONE BY SHARANYA");
System.out.println("PIN:23001-CS-061");
}
} //END OF THE PROGRAM
OUTPUT:
PAGE NAME: G. Sharanya
NO: PIN:23001-CS-061
, JAVA PROGRAMMING CS407
2. Write a java program to implement types of data types available in java
THEORY:
“DATA TYPES” are the things which will specify how a variable will is initialized that is how much
storage it takes and what type of data it holds. In java we are having 2 types of data types
1. Primitive data types
2. Non-primitive data types
PRIMITIVE DATA TYPES:
Integer:
1. Byte : byte stores or allows you to store value between range of -128 to 127
2. Short : short allows you to store values between -32,768 to 32,767
3. Int : int allows you to store values between -2,147,483,648 to 2,147,483,647
4. Long : it is used to store a very large number
Float:
1. Float: it stores a values with precision that means it used represent decimal numbers
with 8bit precision
2. Double: it stores a values with higher precision that is 16 bits
Char: it is used to store a character
String: it is the unique data type in java, which will allow you to store a collection of
characters
Boolean:it is has only two states 1.ture(1) 2.false (0)
Syntax: datatype variable_name=values;
PROGRAM:
class datatypes
{
public static void main(String ar[])
{
String a="SHARANYA";
String fn="Ganesh";
String fm="rani";
byte pin=061;
int marks=999;
PAGE NAME: G. Sharanya
NO: PIN:23001-CS-061
, JAVA PROGRAMMING CS407
short age=16;
long dadsalary=100000000;
float percentage=99.9f;
double cgpa=99999999.999009;
boolean girl=true;
char yesno='s';
System.out.println("student name:"+a);
System.out.println("student father name:"+fn);
System.out.println("student mother name:"+fm);
System.out.println("student pin:"+pin);
System.out.println("student marks:"+marks);
System.out.println("student percentage:"+percentage);
System.out.println("student cgpa:"+cgpa);
System.out.println("student age:"+age);
System.out.println("student parents salary:"+dadsalary);
System.out.println("student is a good mannered person?"+girl);
System.out.println("student is qualified?"+yesno);
}
}
PAGE NAME: G. Sharanya
NO: PIN:23001-CS-061
CHAPTER-1
INTRO TO JAVA
1. Write a java program to implement types of variables in java programming
THEORY:
VARIABLE is nothing but a container or name given to a particular memory location.
Variable declaration is very simple. Simply use the data type name and variable name. If you
want to initialize the values then use “=” and assign the values. In java we are having 3 types of
variables
1. Local variable: the variables whose scope is within a method/at particular spot
2. Instance variable: the variables which are executed under a class using objects
3. Static variable: the variables whose scope is throughout the program and there is no need of
object to initialize it
PROGRAM:
class variable
{
public static int num1=123;
public static int num2=62; //static variable
int x,y; //instance variable
void method()
{
int a=46;
int b=45; //local variables
int c=a-b;
System.out.println("local:"+c);
}
int show(int x,int y)
PAGE NAME: G. Sharanya
NO: PIN:23001-CS-061
, JAVA PROGRAMMING CS407
{
return(x+y);
}
int display(int num1,int num2)
{
return(num1-num2);
}
public static void main(String ar[])
{
variable obj=new variable();
obj.method();
System.out.println("instance:"+obj.show(61,22));
System.out.println("static:"+obj.display(num1,num2));
System.out.println("DONE BY SHARANYA");
System.out.println("PIN:23001-CS-061");
}
} //END OF THE PROGRAM
OUTPUT:
PAGE NAME: G. Sharanya
NO: PIN:23001-CS-061
, JAVA PROGRAMMING CS407
2. Write a java program to implement types of data types available in java
THEORY:
“DATA TYPES” are the things which will specify how a variable will is initialized that is how much
storage it takes and what type of data it holds. In java we are having 2 types of data types
1. Primitive data types
2. Non-primitive data types
PRIMITIVE DATA TYPES:
Integer:
1. Byte : byte stores or allows you to store value between range of -128 to 127
2. Short : short allows you to store values between -32,768 to 32,767
3. Int : int allows you to store values between -2,147,483,648 to 2,147,483,647
4. Long : it is used to store a very large number
Float:
1. Float: it stores a values with precision that means it used represent decimal numbers
with 8bit precision
2. Double: it stores a values with higher precision that is 16 bits
Char: it is used to store a character
String: it is the unique data type in java, which will allow you to store a collection of
characters
Boolean:it is has only two states 1.ture(1) 2.false (0)
Syntax: datatype variable_name=values;
PROGRAM:
class datatypes
{
public static void main(String ar[])
{
String a="SHARANYA";
String fn="Ganesh";
String fm="rani";
byte pin=061;
int marks=999;
PAGE NAME: G. Sharanya
NO: PIN:23001-CS-061
, JAVA PROGRAMMING CS407
short age=16;
long dadsalary=100000000;
float percentage=99.9f;
double cgpa=99999999.999009;
boolean girl=true;
char yesno='s';
System.out.println("student name:"+a);
System.out.println("student father name:"+fn);
System.out.println("student mother name:"+fm);
System.out.println("student pin:"+pin);
System.out.println("student marks:"+marks);
System.out.println("student percentage:"+percentage);
System.out.println("student cgpa:"+cgpa);
System.out.println("student age:"+age);
System.out.println("student parents salary:"+dadsalary);
System.out.println("student is a good mannered person?"+girl);
System.out.println("student is qualified?"+yesno);
}
}
PAGE NAME: G. Sharanya
NO: PIN:23001-CS-061