Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

The java.

lang package:
import java.lang.*;
class Main {
public static void main(String args[]) {
String a = "1230";
int b = Integer.parseInt(a);
System.out.println(b);
}

Handling name conflicts

import java.util.*;
import java.sql.*;

Both contains Date class so we use anyone like this


java.util.Date d1 = new java.util.Date();
java.sql.Date d2= new java.sql.Date();

CLASSPATH can be set by any of the following ways:


CLASSPATH can be set permanently in the environment:
 In Windows choose control panel? System? Advanced? Environment Variables?
 Choose “System Variables” (for all the users) or “User Variables” (only the currently login
user)?
 Choose “Edit” (if CLASSPATH already exists) or “New”? Enter “CLASSPATH” as the
variable name?
 Enter the required directories and JAR files (separated by semicolons) as the value (e.g.,
“.;c:\javaproject\classes;d:\tomcat\lib\servlet-api.jar”). Take note that you need to include the
current working directory (denoted by ‘.’) in the CLASSPATH.

To check the current setting of the CLASSPATH, issue the following command:
 > SET CLASSPATH
CLASSPATH can be set temporarily set at CMD shell session by issuing the following
command:
 > SET CLASSPATH=.;c:\javaproject\classes;d:\tomcat\lib\servlet-api.jar

Creating a package:
To create a package the keyword package is used.
package mypackage;
public class arithmetic
{

41
public int x;
public int y;
public arithmetic(int a,int b)
{
x=a;
y=b;
}
public int add()
{
return(x+y);
}
public int sub()
{
return(x-y);
}
public int mul()
{
return(x*y);
}
public int div()
{
return(x/y);
}
}
Save and compile:
Save the above as arithmetic.java
Accessing a package
To access a package the keyword import is used.
import java.io.*;
import mypackage.arithmetic;
class package1
{
public static void main(String[] args)
{

42
arithmetic ob=new arithmetic(38,25);
System.out.println("sum="+ob.add());
System.out.println("diff="+ob.sub());
System.out.println("mul="+ob.mul());
System.out.println("div="+ob.div());
}
}

Compiling and running:


Save the above main class as package1.java,
Save arithmetic.java in the mypackage subdirectory.
Compiling:
Z:\wt lab>myPackage>javac arithmetic.java
Z:\wt lab>javac package1.java
Run:
Z:\wt lab>java package1

43

You might also like