Unit 1
Unit 1
1
may be a sequence of machine instructions that can be executed by the CPU directly,
or it may be an intermediate representation that is interpreted by a virtual machine.
This intermediate representation in Java is the Java Byte Code.
2
Ternary Operator and, Assignment Operator.
13. Write the syntax for declaration of class and creation of objects?
A class is declared using class keyword. A class contains both data and method that
operate on that data. Thus, the instance variables and methods are known as class
members. When creating an object from a class
Declaration − A variable declaration with a variable name with an object type.
Instantiation − The 'new' keyword is used to create the object.
Initialization − The 'new' keyword is followed by a call to a constructor. This call
initializes the new object.
class Student
{
String name; int rollno; int age;
}
Student std=new Student();
std is instance/object of Student class.
new keyword creates an actual physical copy of the object and assign it to the
std variable.
3
The new operator dynamically allocates memory for an object.
4
17. What do you mean by Dynamic Initialization?
Java is a flexible programming language which allows the dynamic initialization of
variables. In other words, at the time of declaration one can initialize the variables. In
java we can declare the variable at any place before it is used. Example: int a=10;
float d=2.34f;
18. What do you mean by Variable? What are the rules for variable declaration?
Variable is a fundamental unit of storage in java. The variables are used in
combination with identifiers, data types, operators and some value for initialization.
The syntax of variable declaration will be:
data_type name_of_variable[=initialization];
5
There are two types of Constructor
Default Constructor
Parameterized constructor
Each time a new object is created at least one constructor will be invoked.
Car c=new Car(); //Default constructor invoked
Car c=new Car(name); //Parameterized constructor invoked
22. What is array? How to declare array and how to allocate the memory to for array?
Java array contains elements of similar data type. It is a data structure where we store
similar elements. We can store only fixed set of elements in a java array. Array in
java is index based, first element of the array is stored at 0 index.
data_type array_name []; and to allocate the memory-
array_name=new data_type[size];where array_name represent name of the array, new
is a keyword used to allocate the memory for arrays, data_type specifies the data type of
array elements and size represents the size of an array. For example:int a=new int[10];
6
27. What are the control flow statements in java?
A programming language uses control statements to control the flow of execution of
program based on certain conditions. These are used to cause the flow of execution
to advance and branch based on changes to the state of a program.
Java’s Selection statements:
if
if-else
nested-if
if-else-if
switch-case
jump – break, continue, return
These statements allow you to control the flow of your program’s execution based
upon conditions known only during run time.
Prepared By
Rupilaa V M