Assignment
Assignment
1. Static means that you can call the function without having instance of a class(or
creating object of class).
3. The while loop will repeat executing a set of statements infinite number of times
until a certain condition is met.
The do-while loop is similar to the while loop except it checks the condition only
after it runs through its instructions and the do-while loop always runs at least once.
4)The keyword new is used to allocate the memory, so when we ask the compiler to
allocate some memory for some object, it gives it depending upon the size of the object
since the object size is unknown to the compiler unless the program is running.
7) syntax:
public class Main{
static void myMethod() {
//code to be executed
}
example:
8) syntax:
9) The String is immutable, so its value cannot be changed. As Java uses the concept of
String literal. Suppose there are 5 reference variables, all refer to one object. If one
reference variable changes the value of the object, it will be affected by all the reference
variables. That is why String objects are immutable in Java.
10) The stack frame holds the temporary, argument, and return address registers (if they
need to be saved because of a subsequent function call), and any of the saved registers that
the function will modify. It also holds local arrays and any excess local variables.
11) All Strings are stored in the String Pool (or String Intern Pool) that is allocated in the Java
heap. String pool is an implementation of the String Interring Concept. String Interning is a
method that stores only a copy of each distinct string literal. The distinct values are stored in
the String pool.
12) Static memory is allocated for declared variables by the compiler. The address can be
found using address of operator and can be assigned to a pointer. The memory is allocated
during compile time.
13) By string literal: Java String literal is created by using double quotes.
For Example: String s=“Welcome”;
By new keyword: Java String is created by using a keyword “new”.
For example: String s=new String(“Welcome”);
15) Arrays are of fixed length. We cannot change the size of the arrays once they are
created. We cannot accommodate an extra element in an array after they are created.
Memory is allocated to an array during its creation only, much before the actual elements
are added to it.
Because of these drawbacks, use of arrays is less preferred. Instead of arrays, we can use
ArrayList class which addresses all these drawbacks.
Size of the ArrayList is not fixed. ArrayList can grow and shrink dynamically. Elements can be
inserted at or deleted from a particular position. ArrayList class has many methods to
manipulate the stored objects etc.,
16)
class One {
public void display() {
System.out.println("One"); } }
//inheritance
class Two extends One {
@Override
public void display() {
System.out.println("Two");
}
public int add(int x, int y) {
return x+y;
}
//Overload
public double add(double x,double y) {
return x+y; }
}
//encapsulation example
class EncapTest {
private String name;
public String getName()
return name;
}
public void setName(String newName) {
name = newName;
}
}
//abstraction
abstract class TwoWheeler {
public abstract void run();
}
class MainClass {
public static void main(String[] args) {
One a=new One();
a.display();
Two b=new Two();
b.display();
System.out.println(b.add(4,2)); System.out.println(b.add(5.,2.));
//polymorphism
EncapTest encap = new EncapTest(); encap.setName("Tejaswini's");
System.out.print("Name : " + encap.getName() ); TwoWheeler test = new Honda();
test.run();
}
}