0% found this document useful (0 votes)
70 views3 pages

Seminar 1

The document defines and provides examples of local variables, instance variables, static variables, type casting, and debugging in Java. Local variables are defined within methods and only exist within that scope, while instance variables are declared in a class and each object has its own copy. Static variables can be accessed without an object using the class name and are shared among all instances. Type casting involves converting between compatible data types, either automatically during widening casts or manually during narrowing casts. Debugging allows running a program step-by-step and examining variables at breakpoints.

Uploaded by

Ajay Tendulkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views3 pages

Seminar 1

The document defines and provides examples of local variables, instance variables, static variables, type casting, and debugging in Java. Local variables are defined within methods and only exist within that scope, while instance variables are declared in a class and each object has its own copy. Static variables can be accessed without an object using the class name and are shared among all instances. Type casting involves converting between compatible data types, either automatically during widening casts or manually during narrowing casts. Debugging allows running a program step-by-step and examining variables at breakpoints.

Uploaded by

Ajay Tendulkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Local variable: A variable defined with in a method.

-->The scope of the variables exists only with in a method.

EX:public class StudentDetails {


public void StudentAge()
{
// local variable age
int age = 0;
age = age + 5;
System.out.println("Student age is : " + age);
}

public static void main(String args[])


{
StudentDetails obj = new StudentDetails();
obj.StudentAge();
}
}
-->the variable age is a local variable to the function StudentAge().

Instance Variables: Instance variables are non-static variables and are declared in
a class and outside any method.
--> these variables are created when an object of the class is created and
destroyed when the object is destroyed.

Ex:import java.io.*;
class Marks {
// These variables are instance variables.
// These variables are in a class
// and are not inside any function
int engMarks;
int mathsMarks;
int phyMarks;
}

class MarksDemo {
public static void main(String args[])
{
// first object
Marks obj1 = new Marks();
obj1.engMarks = 50;
obj1.mathsMarks = 80;
obj1.phyMarks = 90;

// second object
Marks obj2 = new Marks();
obj2.engMarks = 80;
obj2.mathsMarks = 60;
obj2.phyMarks = 85;

// displaying marks for first object


System.out.println("Marks for first object:");
System.out.println(obj1.engMarks);
System.out.println(obj1.mathsMarks);
System.out.println(obj1.phyMarks);

// displaying marks for second object


System.out.println("Marks for second object:");
System.out.println(obj2.engMarks);
System.out.println(obj2.mathsMarks);
System.out.println(obj2.phyMarks);
}
}
-->the variables engMarks , mathsMarks , phyMarks are instance variables.
-->we have multiple objects as in the above program, each object will have its own
copies of instance variables.

Static variable: static variables are declared using the static keyword within a
class outside any method.
-->need not create an object of that class.
-->To access static variables-->class_name.variable_name;

EX:import java.io.*;
class Emp {

// static variable salary


public static double salary;
public static String name = "Harsh";
}

public class EmpDemo {


public static void main(String args[])
{

// accessing static variable without object


Emp.salary = 1000;
System.out.println(Emp.name + "'s average salary:"
+ Emp.salary);
}
}

Type Casting:is a method or process that converts a data type into another data
type in both ways manually and automatically.
-->The automatic conversion is done by the compiler and manual conversion performed
by the programmer.

There are two types of type casting:

Widening Type Casting:


-->Converting a lower data type into a higher one is called widening type casting.
-->Both data types must be compatible with each other.
-->byte -> short -> char -> int -> long -> float -> double.

EX:public class WideningTypeCastingExample


{
public static void main(String[] args)
{
int x = 7;
//automatically converts the integer type into long type
long y = x;
//automatically converts the long type into float type
float z = y;
System.out.println("Before conversion, int value "+x);
System.out.println("After conversion, long value "+y);
System.out.println("After conversion, float value "+z);
}
}

Narrowing Type Casting:


-->Converting a higher data type into a lower one is called narrowing type casting.
-->It is done manually by the programmer.
-->double -> float -> long -> int -> char -> short -> byte

EX:public class NarrowingTypeCastingExample


{
public static void main(String args[])
{
double d = 166.66;
//converting double data type into long data type
long l = (long)d;
//converting long data type into int data type
int i = (int)l;
System.out.println("Before conversion: "+d);
//fractional part lost
System.out.println("After conversion into long type: "+l);
//fractional part lost
System.out.println("After conversion into int type: "+i);
}
}

Debugging:
-->it allows you to run a program interactively while watching the source code and
the variables during the execution.
-->A breakpoint in the source code specifies where the execution of the program
should stop during debugging.

You might also like