Professional Documents
Culture Documents
JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime
environment in which java bytecode can be executed.
• Mobile Applications
• Desktop GUI Applications
• Web-based Applications
• Enterprise Applications
• Scientific Applications
• Gaming Applications
• Big Data technologies
• Business Applications
• Distributed Applications
• Cloud-based Applications
A variable is a container which holds the value while the Java program is executed. A
variable is assigned with a data type.
Variable is a name of memory location. There are three types of variables in java: local,
instance and static.
There are two types of data types in Java: primitive and non-primitive.
Local Variable
A variable declared inside the body of the method is called local variable. You can use this
variable only within that method and the other methods in the class aren't even aware that the
variable exists.
2) Instance Variable
A variable declared inside the class but outside the body of the method, is called instance
variable. It is not declared as static.
It is called instance variable because its value is instance specific and is not shared among
instances.
3) Static variable
A variable which is declared as static is called static variable. It cannot be local. You can
create a single copy of static variable and share among all the instances of the class. Memory
allocation for static variable happens only once when the class is loaded in the memory.
The process of converting the value of one data type (int, float, double, etc.) to another
data type is known as typecasting.
1.Widening Type Casting--Converting a lower data type into a higher one is called widening type
casting. It is also known as implicit conversion or casting down.
byte -> short -> char -> int -> long -> float -> double
2. Narrowing Type Casting-- Converting a higher data type into a lower one is called narrowing
type casting. It is also known as explicit conversion or casting up
double -> float -> long -> int -> char -> short -> byte
5. Explain Java String Methods with an example.
In Java, string is basically an object that represents sequence of char values. An array of
characters works same as Java string.
Java String class provides a lot of methods to perform operations on strings such as
compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring()
etc.
6. Explain increment and decrement operator with its types and example
class Operator {
public static void main(String[] args) {
int var1 = 5, var2 = 5;
// 5 is displayed
// Then, var1 is increased to 6.
System.out.println(var1++);
// var2 is increased to 6
// Then, var2 is displayed
System.out.println(++var2);
} }
the increment operator ++ increases the value of a variable by 1. Similarly, the decrement operator
-- decreases the value of a variable by 1.
7. Explain if statement with its syntax, control flow diagram and example.
Syntax
if(Boolean_expression) {
// Statements will execute if the Boolean expression is true
}
If the Boolean expression evaluates to true then the block of code inside the if statement will
be executed. If not, the first set of code after the end of the if statement (after the closing
curly brace) will be executed.
Flow Diagram
Example
Live Demo
public class Test {
if( x < 20 ) {
System.out.print("This is if statement");
}
}
}
This will produce the following result −
Output
This is if statement.
If the condition is true, it will execute expression 1 else it will execute expression 2.
A loop on the other hand will do the same instructions again and again until the condition is
satisfied. Loops include while, do while and for.
9. Explain Method Overriding in java with its example. What are the rules for
Declaring a method in sub class which is already present in parent class is known as method
overriding. Overriding is done so that a child class can give its own implementation to a method
which is already provided by the parent class. In this case the method in parent class is called
overridden method and the method in child class is called overriding method.