Posts

java assignment Question 6

6.What is reference variable? Ans- The only way you can access an object is through a  reference variable . A reference variable is declared to be of a specific type and that type can never be changed. Reference variables can be declared as static variables, instance variables, method parameters, or local variables. A reference variable that is declared as final can’t never be reassigned to refer to a different object. The data within the object can be modified, but the reference variable cannot be changed. Types of reference variables Static Variable Instance Variable Method Parameter Local Variable When you create an object of a class as − Student obj = new Student(); The objects are created in the heap area and, the reference  obj  just points out to the object of the Student class in the heap, i.e. it just holds the memory address of the object (in the heap). And since the String is also an object, under name, a reference points out to th...

java Assignment Questions 4

4.What is Constructor?write down the types of constructor.  Ans:- 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 and attributes.  Rules for writing Constructor: Constructor(s) of a class must has same name as the class name in which it resides. A constructor in Java can not be abstract, final, static and Synchronized. Access modifiers can be used in constructor declaration to control its access i.e which other class can call the constructor. Types of constructor There are two type of constructor in Java: No-argument constructor:  A constructor that has no parameter is known as default constructor. If we don’t define a constructor in a class, then compiler creates  default constructor(with no arguments)  for the class. And if we write a constructor with arguments or no-arguments then the compi...

java Assignment Questions 5

5.Difference between Constructor And Method. Ans:-   1.Constructor is used to initialize an object whereas method is used to exhibits functionality of an object. 2.Constructors are invoked implicitly whereas methods are invoked explicitly. 3.Constructor does not return any value where the method may/may not return a value. 4.In case constructor is not present, a default constructor is provided by java compiler. In the case of a method, no default method is provided. 5.Constructor should be of the same name as that of class. Method name should not be of the same name as that of class.
3.Differentiate between String and string Buffer. Ans:- String:- String class is immutable . String is slow and consumes more memory when you concat too many strings because every time it creates new instance. 3.String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method . Example:- public class StringDemo{ public static void main(String args[]){ char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'}; String helloString = new String(helloArray); System.out.println( helloString ); } } OUTPUT:- hello String Buffer:- StringBuffer class is mutable . StringBuffer is fast and consumes less memory when you cancat strings. StringBuffer class doesn't override the equals() method of Object class.
2.Difference between static and non-static variables. Ans:- Static Variables:-  When a variable is declared as static, then a single copy of the variable is created and shared among all objects at a class level. Static variables are, essentially, global variables. All instances of the class share the same static variable . Static variables can be accessed using class name Static variables can be accessed by static and non static methods. Static variable is like a global variable and is available to all methods. Non-static Variable:- Non static variables can be accessed using instance of a class. Non static variables cannot be accessed inside a static method. Non static variables do not reduce the amount of memory used by a program. Non static variables are specific to that instance of a class. Non static variable is like a local variable and they can be accessed through only instance of a class.