Datatypes
Datatypes
Course: Core Java Unit: I Prepared by: Ms. Prajakta Joshi/ Ms. Srushty Padate
__________________________________________________________________________________
The code written in Java, is converted into byte codes which is done by the Java Compiler.
The byte codes, then are converted into machine code by the JVM.
The Machine code is executed directly by the machine.
Java Virtual Machine, Java Runtime Environment and Java Development Kit respectively.
Explanation:
Class Loader: Class loader is a subsystem of JVM. It is used to load class files. Whenever we run the
java program, class loader loads it first.
Class method area: It is one of the Data Area in JVM, in which Class data will be stored. Static
Variables, Static Blocks, Static Methods, Instance Methods are stored in this area.
Heap: A heap is created when the JVM starts up. It may increase or decrease in size while the
application runs.
Stack: JVM stack is known as a thread stack. It is a data area in the JVM memory which is created for
a single execution thread. The JVM stack of a thread is used by the thread to store various elements
i.e.; local variables, partial results, and data for calling method and returns.
Native stack: It subsumes all the native methods used in your application.
Execution Engine:
JIT compiler
Garbage collector
JIT compiler: The Just-In-Time (JIT) compiler is a part of the runtime environment. It helps in
improving the performance of Java applications by compiling bytecodes to machine code at run time.
The JIT compiler is enabled by default. When a method is compiled, the JVM calls the compiled code
of that method directly. The JIT compiler compiles the bytecode of that method into machine code,
compiling it “just in time” to run.
Garbage collector: As the name explains that Garbage Collector means to collect the unused material.
Well, in JVM this work is done by Garbage collection. It tracks each and every object available in the
JVM heap space and removes unwanted ones.
Garbage collector works in two simple steps known as Mark and Sweep:
Mark – it is where the garbage collector identifies which piece of memory is in use and which
are not
Sweep – it removes objects identified during the “mark” phase.
First source code is used by java compiler and is converted in .class file. The class file code is in byte
code form and that class file is used by JVM to convert into an object file. After that, you can see the
final output on your screen.
Java Identifiers
In programming languages, identifiers are used for identification purpose. In Java, an
identifier can be a class name, method name, variable name or a label.
Example:
The only allowed characters for identifiers are all alphanumeric characters([A-Z],[a-z],[0-9]),
‘$‘(dollar sign) and ‘_‘ (underscore).For example “geek@” is not a valid java identifier as it contain
‘@’ special character.
Identifiers should not start with digits([0-9]). For example “123geeks” is a not a valid java identifier.
Java identifiers are case-sensitive.
There is no limit on the length of the identifier but it is advisable to use an optimum length of 4 – 15
letters only.
Reserved Words can’t be used as an identifier. For example “int while = 20;” is an invalid
statement as while is a reserved word. There are 53 reserved words in Java.
Compiler vs Interpreter
Compiler and Interpreter are two different ways to execute a program written in a programming or
scripting language.
A compiler takes entire program and converts it into object code which is typically stored in a
file. The object code is also refereed as binary code and can be directly executed by the machine
after linking. Examples of compiled programming languages are C and C++.
1) Both compilers and interpreters convert source code (text files) into tokens, both may generate
a parse tree, and both may generate immediate instructions. The basic difference is that a compiler
system, including a (built in or separate) linker, generates a stand alone machine code program,
while an interpreter system instead performs the actions described by the high level program.
2) Once a program is compiled, its source code is not useful for running the code. For interpreted
programs, the source code is needed to run the program every time.
4) Java programs are first compiled to an intermediate form, then interpreted by the interpreter.
Operators in Java
Operator in Java is a symbol which is used to perform operations. For example: +, -, *, / etc.
There are many types of operators in Java which are given below:
o Unary Operator,
o Arithmetic Operator,
o Shift Operator,
o Relational Operator,
o Bitwise Operator,
LSRC/2019-20/ HTML & CSS Page 4
[datatypes] April 1, 2020
o Logical Operator,
o Assignment Operator.
Data types specify the different sizes and values that can be stored in the variable. There are two
types of data types in Java:
1. Primitive data types: The primitive data types include boolean, char, byte, short, int,
long, float and double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces,
and Arrays.
NOTE: ALL THE DATA TYPES SHOULD BE EXPLAINED WITH THEIR SIZE,
RANGE.
Course: Core Java Unit: II Prepared by: Ms. Prajakta Joshi/ Ms. Srushty
Padate
_________________________________________________________________________________________
CONTROL FLOW STATEMENTS IN JAVA
Classes:
What are the different types of Classes in Java?
POJO Class.
Static Class.
Concrete Class.
Abstract Class.
Final Class.
Inner Class. Nested Inner class. Method Local inner classes. Anonymous inner classes. Static
nested classes.
NOTE: ALL TYPES SHOULD BE EXPLAINED WITH AN EXAMPLE.
Initializing an object means storing data into the object. Let's see a simple example where we are going
to initialize the object through a reference variable.
In this example, we are creating the two objects of Student class and initializing the value to these
objects by invoking the insertRecord method. Here, we are displaying the state (data) of the objects by
invoking the displayInformation() method.
Method Overloading is a feature that allows a class to have more than one method having the same
name, if their argument lists are different. It is similar to constructor overloading in Java, that allows a
class to have more than one constructor having different argument lists.
let’s get back to the point, when I say argument list it means the parameters that a method has: For
example the argument list of a method add(int a, int b) having two parameters is different from the
argument list of the method add(int a, int b, int c) having three parameters.
In order to overload a method, the argument lists of the methods must differ in either of these:
1. Number of parameters.
Example: This is a valid case of overloading
add(int, int)
add(int, int, int)
EXAMPLE:
class DisplayOverloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c + " "+num);
}
}
class Sample
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading();
obj.disp('a');
obj.disp('a',10);
}
}
Java Constructors
A constructor in Java is a special method that is used to initialize objects. The constructor is called
when an object of a class is created. It can be used to set initial values for object attributes:
Example
Create a constructor:
// Create a MyClass class
public class MyClass {
int x; // Create a class attribute
Garbage Collection is process of reclaiming the runtime unused memory automatically. In other words,
it is a way to destroy the unused objects.
To do so, we were using free() function in C language and delete() in C++. But, in java it is performed
automatically. So, java provides better memory management.
It makes java memory efficient because garbage collector removes the unreferenced objects
from heap memory.
It is automatically done by the garbage collector(a part of JVM) so we don't need to make
extra efforts.
finalize() method
The finalize() method is invoked each time before the object is garbage collected. This method can be
used to perform cleanup processing. This method is defined in Object class as:
gc() method
The gc() method is used to invoke the garbage collector to perform cleanup processing.
The gc() is found in System and Runtime classes.
NOTE: THESE POINTS ARE INDICATIVE AND NOT EXHAUSTIVE. PLEASE ELLABORATE THE ANSWERS WITH
PROPER EXAMPLES WHEREVER APPLICABLE.
1 Write a java code to display grade of students based on basic grading system using switch case
statement.
2 Differentiate between while loop and do while loop.
3 What are methods and what is method overloading?
4 Write a short note on access specifies in java.
5 Explain the functionality of different iterative statements with suitable examples.
6 What is a constructor and characteristics of a constructor.?
7 Write a code to illustrate the working of method overloading.
8 List and explain types of classes in java.
9 What is garbage collection in java and how is it helpful?
10 When do you use keywords final and static? Explain.
11 What is a class object and what are its attributes?
12 What are characteristics of a member of a class?
13 Explain if else with an example.
14 Write a code in java to illustrate if statement and nested if statement.
15 Write a code to print pattern using for loop.
@@@@@
@@@@
@@@
@@
@