Module1 - Chapter5 - Core Java
Module1 - Chapter5 - Core Java
MANUAL V8.3
MODULE CODE:
DL.A.01.01
ANUDIP FOUNDATION
Trainer
Manual Core Java
1
Trainer
Manual Core Java
Chapter 5
2
Trainer
Manual Core Java
Chapter 5
Java Variables
A variable in Java refers to a container which holds a value during java program execution. A Java variable has to have
a data type assigned to it. It is a memory piece capable of containing a data value. In simpler terms, a variable is the
Example of a Java variable - int data=60;→ ‘data’ is the variable here and ‘int’ is the data type.
i) local variable
i) Local Variable
A local variable is a variable declared within the body of a method. This variable type can be used only within its
designated method. Only a variable’s own method is aware of it, and not other methods. Using a ‘static’ keyword
to define a local variable is not possible. Default values are not assigned to local variables.
int area()
3
Trainer
Manual Core Java
return rectarea;
An instance variable is a variable declared within a class, but outside of a method body. It is similar to a class variable
in Java. The value of an instance variable is specific to an ‘instance’, and not shared with other instances. Access
modifiers can be assigned to these variables. An instance variable cannot be declared as ‘static’. This variable is
class Taxes
{
int count; → Count is an Instance variable
/*...*/
A static variable is a variable declared with a ‘static’ keyword. A static variable’s copy can be shared with all
instances of a class. Static variable memory allocation only happens once, during the loading of a class in the memory.
This variable type is not local. It is used for achieving shared properties of all objects.
4
Trainer
Manual Core Java
class B{
int data=60; → instance variable
void method(){
}
}
Declaring a variable stores a value in the Assigned variable signals a compiler to assign
memory assigned to a variable. variable memory in stack.
Type casting
Type casting in Java is used for converting a variable or object type to another. It involves the assigning of a primitive
5
Trainer
Manual Core Java
i) Narrowing type casting - Narrowing type casting, also known as downcasting, is a data type conversion process
used for -
6
Trainer
Manual Core Java
System.out.println(myDouble);
System.out.println(myInt);
Output: 8.64
ii) Widening type casting - Widening type casting, also known as upcasting, is a data type conversion process taking
place when -
* A subclass reference variable gets automatically placed within its superclass reference variable
* A small primitive type value gets automatically placed within a bigger primitive data type
int myInt = 6;
System.out.println(myInt);
System.out.println(myDouble);
7
Trainer
Manual Core Java
Output: 6
6.0
ToString() - toString() is a method to return an object’s string representation. The Java compiler invokes the
toString() method when an object is printed. This method is overridden to obtain the required output. Overriding the
toString() method of an Object class is efficient for returning object values, and also reduces the coding one has to do.
class Employee{
int empcode;
String name;
String city;
System.out.println(e1);
System.out.println(e2);
}
}
8
Trainer
Manual Core Java
Output-
350 Rohan Kolkata
toArray() -The toArray() method is used to fetch an array containing all the elements of an ArrayList object in an
orderly sequence (arranged from first to last).
9
Trainer
Manual Core Java
Output:
D:\java>javac test.java
D:\java>java test
Size of list: 5
Color = Red
Color = Blue
Color = Green
Color = Yellow
Color = Orange
Print elements from first to last:
Color = Red
Color = Blue
Color = Green
Color = Yellow
Color = Orange
Parsing
Parsing is a process by which a data type value is converted to another data type value. In Java, parsing is the
conversion of a string to a primitive data type. It is an important process as string values cannot be used for
arithmetic operations. The two types of parsing are top down and bottom up parsing.
Practical(60 minutes)
a) See the example programme of narrow typecasting below. Write a similar programme for narrowing down 10.46
10
Trainer
Manual Core Java
System.out.println(myDouble);
System.out.println(myInt);
b) See the example programme of wide typecasting below. Write a similar programme for widening the values 10.46
int myInt = 6;
System.out.println(myInt);
System.out.println(myDouble);
11
Trainer
Manual Core Java
Instructions: The progress of students will be assessed with the exercises mentioned below.
MCQ
a) value
b) file system
c) file
a) array
b) data
c) 30
d) 10
a) var
b) dynamic
c) static
12
Trainer
Manual Core Java
a) a byte
b) a block
c) a bracket
a) static
b) local
c) instance
6. Static variable memory allocation only happens during the loading of a class in the ________.
a) directory
b) memory
c) array
13
Trainer
Manual Core Java
a) broadcasting
b) upcasting
c) downcasting
9. Narrow casting is used for narrowing a wider __________ data type value.
a) non-primitive
b) definitive
c) primitive
d) multiple
14