Java Lang Package Tutorial


 Java.lang Package Notes

 

 

Wrapper Classes

Sometimes it is necessary to convert a simple primitive datatype into an object. For this, each primitive datatype contains its own class. This is called Wrapper class and contains methods to convert primitive to object and object to primitive. Below you will find a list of data types and their classes.

 

Datatype        Class

short------Short
int--------Integer
long-------Long
float------Float
double------Double
byte-------Byte
char-------Character
boolean----Boolean

java.lang  package classes hierarchy

Except Boolean class, All classes are child classes of Number class. 

Converting primitive to object

ex.
 
 Integer iob = new Integer(100);
 

Converting object to primitive

Menu

ex.
 
int x = iob.intValue();
 
 
class Wrapperdemo{
public static void main(String args[]){
Integer iob = new Integer(10);
Double db = new Double(10.5d);
int i = iob.intValue();
double d = db.doubleValue();
System.out.println("i="+i);
System.out.println("d="+d);
}
}

Result

C:\javalang>java Wrapperdemo
i=10
d=10.0
b=10

Goto Menu

Autoboxing and auto unboxing 

Conversion takes place implicitly between the primitive types and their corresponding object wrapper classes at compile time. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes in reverse, then it is called unboxing.

ex:
class Boxunbox
{
int m1(Integer v){
return v;
}
public static void main(String args[]){
 
    Integer iob;
    Double dob=96.8;
    iob = 7;
    int i=iob;
        System.out.println(iob+" "+i);
        System.out.println(iob++);
        dob=iob+dob;
        System.out.println(dob); 
 
 }
}
 
Result:
C:\javalang>java Boxunbox
7 7
8
p=8 

Main Menu 

autoboxing in switch


Interger iob=2
 
switch(iob)
{
case 1:
    System.out.println("one");
    break;
case 2:
    System.out.println("two");
    break;
}

JAVA.LANG PACKAGE  CLASSES

 

Character class


public class Chartest
{
    public static void main(String args[]){
    char ch='a';
    if(Character.isDigit(ch))
        System.out.println(ch + " is a digit.");
    if(Character.isLetter(ch))
        System.out.println(ch + " is a Letter.");
    if(Character.isWhitespace(ch))
        System.out.println(ch + " is a Whitespace.");
    if(Character.isUpperCase(ch))
        System.out.println(ch + " is a UpperCase.");
    if(Character.isLowerCase(ch))
        System.out.println(ch + " is a LowerCase.");
}
} 
Result:
a is a Letter.
a is a LowerCase.
 

Process class

Each task is represented as a one process. 

ex: if notepad is opened means one process is running.

Runtime class

Represents the current runtime environment(RAM).

Menu

Memory Management


public class MemoryDemo
{
public static void main(String args[])
{
	Runtime r = Runtime.getRuntime();
	long mem1,mem2; 
 
       Integer intarray[] =new Integer[1000];
	    System.out.println("Total memory is: " + r.totalMemory());
	mem1=r.freeMemory();
	    System.out.println("Initial free Memory:" + mem1); 

       for(int i=0;i<1000;i++)
    intarray[i] = new Integer(i);
 
       for(int i=0;i<1000;i++)
	    System.out.print(intarray[i] + " "); 
 
       mem2 = r.freeMemory();
	    System.out.println(" Free Memory after Allocation:" + mem2);
	    System.out.println(" Memory used by Allocation:" + (mem1-mem2);
 
       for(int i = 0;i<1000;i++)
	    intarray[i] = null;
	r.gc();
 
       mem2=r.freeMemory();
	    System.out.println(" Free Memory after collecting:" + mem2);
}}	


Result:

C:\javalang>javac MemoryDemo.java
C:\javalang>java MemoryDemo
Total memory is: 64487424
Initial free Memory:62783696
 Free Memory after Allocation:62783696
 Memory used by Allocation:0
 Free Memory after collecting:63985344


Menu

Executing Other Application Using Java Program:


public class ExecDemo
{
public static void main(String args[])
{
Runtime r = Runtime.getRuntime();
Process p=null;
try{
p=r.exec("notepad.exe");
}catch(Exception e){
    System.out.println("Error in executing Application.");
}
}
}

Result:

it Opens a notepad

Goto TopMenu

System class


contains 3 static variable.
System.out.
System.in.
System.err

 

methods in System class


getProperty()
 
import java.util.*;
public class Demo
{
public static void main(String args[])
{
    String s1;
    System.out.println(System.getProperty("user.dir"));
    System.out.println(System.getProperty("user.home"));
    System.out.println(System.getProperty("java.home"));
    System.out.println(System.getProperty("java.vendor.url"));
    System.out.println(s1.getClass());
}}
 
Reult:
 
C:\javalang>java Demo
Time taken to complete for Loop
Elapsed time:15
C:\javalang
C:\Users\condingzonPC
C:\Program Files\Java\jdk1.8.0_25\jre
http://java.oracle.com/
class java.lang.String 

Object class


super class of all java classes

Class class

Menu↑

We can get the  object's Class by calling the getClass() method defined by Object 

ex:

class X{
    int a;
    float b;
}
class Y extends X{
    double c;
}
class Testclass{
    public static void main(String args[])
    {
        X xob = new X();
        Y yob = new Y();
        Class clObj;
        clObj = xob.getClass();
            System.out.println("xob is object of " + clobj.getName());
        clObj = yob.getClass();
            System.out.println("y is object of " + clObj.getName());
        clObj = clObj.getSuperclass();
            System.out.println("y's super class is:" + clObj.getName());
}
}
 
Result:
C:\javalang>java Testclass
x is object of type:X
y is object of type:Y
y's super class is:X 
 

Math class

variable

PI 

Methods in Math Class

abs() returns you the absolute number.  

ceil() returns you the smallest value but greater than the argument.

 exp() returns you the exponential value raised to the power of a double value. 

floor() returns you the largest value but less than the argument. 

max() maximum value from the two given value.  

min() minimum value from the two given value.  

pow() returns the power of a first given value by the another one. 

random() returns you the random number. It is absolutely system generated. 

round() returns you a value that is in the rounded form.

 sqrt() returns you the square root of the specified value.

Menu

 
public class MathTest{
public static void main(String[] args){
	System.out.println("PI = " + Math.round(Math.PI*100)/100f);
	System.out.println("Absolute number = " + Math.abs(Math.PI));
	System.out.println("ceil = " + Math.ceil(Math.PI));
	System.out.println("Exponent= " + Math.exp(0));
	System.out.println("floor = " + Math.floor(Math.E));
	System.out.println("Maximum r = " + Math.max(10,10.4));
	System.out.println("Minimum = " + Math.min(10,10.4));
	System.out.println("Power = " + Math.pow(10,4));
	System.out.println("Random Num = " + Math.random());
       System.out.println("Round Num = " + Math.round(123.4567));
       System.out.println("Square Root = " + Math.sqrt(9));
}
}
 
Result:
C:\javalang>java MathTest
pi = 3.14
Absolute number = 3.141592653589793
Smallest value but greater than the argument = 4.0
Exponent number powered by the argument = 1.0
Largest value but less than the argument = 2.0
Maximum Number = 10.4
Minimum Number = 10.0
Power = 1000.0
Random Number = 0.8626008087870752
Round = 123
Square Root = 3.0 

Enum class

Enumaration: contains a collection of values. Starts with keyword enum.

 
 
import java.util.*;
 
 enum Cources{Java, PHP, Python, JavaScript}
 
public class EnumExample {
 
public static void main(String args[])
{
Cources course;
course = Cources.Java;
switch(course) {
case Java:
    System.out.println("You chose Java!");
break;
case PHP:
    System.out.println("You chose PHP!");
break;
case Python:
    System.out.println("You chose Python!");
break;
case JavaScript:
    System.out.println("You chose JavaScript!");
break;
default:
    System.out.println("Not in a Course List.");
break;
}
}
} 

 

Result:

C:\javalang>java EnumTest
You chose Java!

 

 

 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP