Sarojini Naidu Govt Girls P.G. College: Bca Vi Sem. Cce I Java

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

SAROJINI NAIDU GOVT GIRLS

P.G.  COLLEGE
BCA VI SEM.
CCE I
JAVA

SUBMITTED TO SUBMITTED BY

DHEERAJ SIR SUMITA KUMARI


ROLL CALL - 39
DECLARATION
OF VARIABLES
We can declare variables in java as follows:
We can declare variables in java as follows:

datatype: Type of data that can be stored in this variable.


variable_name: Name given to the variable.
value: It is the initial value stored in the variable.

Examples

float simpleInterest; //Declaring float variable


int time = 10, speed = 20; //Declaring and Initializing integer variable
char var = 'h'; // Declaring and Initializing character variable
Scope of Variables

 Scope of a variable is the part of the


program where the variable is accessible.
Like C/C++, in Java, all identifiers are
lexically (or statically) scoped, i.e.scope
of a variable can determined at compile
time and independent of function call Member Variables Local Variables
stack. (Class Level (Method Level
Java programs are organized in the form Scope) Scope)
of classes. Every class is part of some
package.
Java scope rules can be covered under following categories.

Member Variables (Class Level Scope)

These variables must be declared inside class (outside any function). They can be directly accessed anywhere in
class. Let’s take a look at an example:
public class Test
{
    // All variables defined directly inside a class
    // are member variables
    int a;
    private String b
    void method1() {....}
    int method2() {....}
    char c;
}
We can declare class variables anywhere in class, but outside methods.

Access specified of member variables doesn’t effect scope of them within a class.

Member variables can be accessed outside a class with following rules:

  Modifier      Package  Subclass  World

public          Yes      Yes     Yes

protected       Yes      Yes     No

Default (no
modifier)       Yes       No     No

private         No        No     No


Local Variables (Method Level Scope)
Variables declared inside a method have method level scope and can’t be accessed outside the method.

public class Test


{
    void method1()
    {
       // Local variable (Method level scope)
       int x;
    }
}

 Local variables don’t exist after method’s execution is over.


Symbolic Constants

 A symbolic constant is an "variable" whose value does not change during the entire
lifetime of the program...
Some constants in  Mathematics:

Name of the constant Value of the constant


Pi 3.1415926535...
e (Natural log) 2.718281828...

Some constants in Physics:

Name of the constant Value of the constant


Gravitational Constant 6.67300 × 10-11 (m3 kg-1 s-2)
c (speed of light) 299,792,458 (m/sec)
Syntax to define a constant:

A symbolic constant can be defined:

inside a method - such a constant can only be used by the method in which it is defined, or
inside a class (outside all methods) - the access of such a constant is determined by the given access
specifier

Syntax to define a constant inside a method:


final typeName variableName = expression;

    

Syntax to define a constant inside a class (outside methods):


accessSpecifier static final typeName variableName = expression;
Type Casting

Assigning a value of one type to a variable of another


type is known as Type Casting.

Example :
int x = 10;
 byte y = (byte)x;
In Java, type casting is classified
into two types,
1.Widening Casting(Implicit)       

2.Narrowing Casting(Explicitly done)


Widening or Automatic type converion
Automatic Type casting take place when,the two types are compatible and when the
target type is larger than the source type
Example :
public class Test
{
    public static void main(String[] args)
    {
      int i = 100;
      long l = i; //no explicit type casting required
      float f = l; //no explicit type casting required
      System.out.println("Int value "+i);
      System.out.println("Long value "+l);
      System.out.println("Float value "+f);
    }

}
Narrowing or Explicit type conversion

When you are assigning a larger type value to a variable of smaller type, then you need to perform explicit
type casting.
Example :
public class Test

    public static void main(String[] args)

    {

      double d = 100.04;

      long l = (long)d;  //explicit type casting required

       int i = (int)l; //explicit type casting required

       System.out.println("Double value "+d);

      System.out.println("Long value "+l);

      System.out.println("Int value "+i);

    }

You might also like