Java

Wrapper Class in Java

Posted on

Wrapper Classes

A wrapper class is a class whose object wraps or contains a primitive data type. When we create an object to a wrapper class, it contains a field and in this field, we can store a primitive data type. For example, if we create an object to Character wrapper class, it contains a single field char and it is possible to store a character like A there.

Primitive Data Type

Corresponding Wrapper Class

char

Character

byte

Byte

short

Short

int

Integer

long

Long

float

Float

double

Double

boolean

Boolean

 

Conclusion:

Since OOPs is all about objects, it is necessary to convert everything into objects. Wrapper classes are useful to convert primitive data types into objects. Moreover, they contain methods to reconvert an object into primitive data.

 

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

Advertisements

Abstract Window ToolKit

Posted on

Using drawing tool in abstract window toolkit :

import java.awt.*;

import java.awt.event.*;

class home extends Frame

{

home()

{

 this. addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

}

public void paint(Graphics g)

{

 int x[]={375,275,475};

int y[]={125,200,200};

int n=3;

this.setBackground(Color.gray);

g.setColor(Color.yellow);

g.fillRect(300,200,150,100);

g.setColor(Color.blue);

g.fillRect(350,210,50,60);

g.drawLine(350,280,400,280);

g.setColor(Color.darkGray);

g.fillPolygon(x,y,n);

g.setColor(Color.cyan);

g.fillOval(100,100,60,60);

g.setColor(Color.green);

g.fillArc(50,250,150,100,0,180);

g.fillArc(150,250,150,100,0,180);

g.fillArc(450,250,150,100,0,180);

g.drawLine(50,300,600,300);

g.drawString(“My Happy Home”,275,350);

}

public static void main(String args[])

{

home h=new home();

h.setSize(500,400);

h.setTitle(“My Home”);

h.setVisible(true);

}

}

output 

1

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

 

core java

Posted on

Search topic: Question and answer on java

  1. Can you call the main() method of  a class from another class?
  2. yes. We can call the main() method of  a class from another class using classname. Main(). At the time of calling the main() method, we should pass a string type array to it.

 

  1. Is string a class or data type?
  2. String is a class in java.lang.package. But in java, all classes are also considered as data types. So we can take String as a data type also.

 

  1. Out of do…while and while- which loop is efficient?
  2. In a do..while loop, the statements are executed without testing the condition, the first time. From the second time only the condition is observed. This means that the programmer does not have control right from the beginning of its execution. In a while loop, the condition is tested first and then only the statements are executed. This means it provides better control right from the beginning. Hence while is more efficient than do…while loop.

 

  1. What is the difference between System.exit(0) and System.exit(1)?
  2. System.exit(0) terminates the program normally. Whereas System.exit(1) terminates the program because of some error encountered in the program.

 

  1. On which memory, arrays are created in Java?
  2. Arrays are created on dynamic memory by JVM. There is no question of static memory in java, everything is created on dynamic memory only.

 

  1. Explain the difference between the following two statements:
  2. String s=”hello”;
  3. String s=new String(“Hello”);

In the first statement, assignment operator is used to assign the string literal to the String variable s. In this case, JVM first of all checks whether the same object is already available in the string constant pool. If it available, then it creates another reference to it. If the same object is not available, then it creates another object with the content “hello” and stores it into the string constant pool.

In second statement, new operator is used to create the string object. In this case, JVM always creates a new object without looking in the string constant pool.

  1. Can you declare a class as ‘private’?
  2. No. if we declare a class as private. Then it is not available to java complier and hence a compile time error occurs. But, inner classes can be declared as private.

 

  1. What are factory methods?
  2. A factory method is a method that creates and returns an object to the class to which it belongs. A single factory method replaces several constructors in the class by accepting different options from the user, while creating the object.

 

  1. What is object graph?
  2. Object graph is a graph showing relationship between different objects in memory.

 

  1. What is anonymous inner class?
  2. It is an inner class whose name is not written in the outer class and for which only one object is created.

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com