Let Us Know about Java Programming in Details with Examples


This resource contains the information about java programming, essentials tips for writing program, examples etc.

Let Us Know about Java Programming in Details with Examples



While writing program we should know some essential tips



1. In case of java predefined class names, every substring first letter should be capital.
Eg: DataInputStream, String

2. In case of java predefined method names, second substring onwards every substring first letter is capital.
Eg: itemStateChanged(), print()

3. Java does not support standalone methods. Everything should be included in class.

4. It is case sensitive.

5. Main method class name and program file primary name should be same.
6. java.lang is default importing package of java

7. Java is pure object oriented programming. Nothing is developed without class concept.
Example:
MyClass.java

class MyClass
{
public static void main(String args[])
{
System.out.print("hello");
}
}


public static void main(String args[])



public: public members are called from anywhere in the computer. These are global members. Interpreter calls main() method from outside the program. Due to this, it is public.

static: static methods are called by class name and nonstatic methods are called by object name. Main method is called by class name. Due to this, it is static.

java MyClass:Interpreter takes primary name of .class file and enters into that file. Then it searches for the class which is having same name as primary name of .class file. Then it searches for main method in that class and executes that method.

void: main method does not return any value. Due to this it is void.

String args[]: One String object can takes one string.
String object array can handle multiplies strings. main() method contains dynamic String object array. It handles cammandline argument values.



PrintStream class



System.out.print("hello");

class PrintStream
{
print();
println();
}
class System
{
static PrintStream out;
}

PrintStream class contains print(), println() methods. System class contains static object of PrintStream class type, with the name out. System class name is used to call static member out. out object is used to call nonstatic method print(). print() method displays given string on the monitor.


println() method



class MyClass1
{
public static void main(String args[])
{
System.out.print("Welcome to ");
System.out.println("nagpur");
System.out.print("by raju");
}
}

println() method displays given string at current location, then pushes cursor to next line.

length keyword



class CmdLine
{
public static void main(String args[])
{
System.out.print("Count of values:"+args.length);
System.out.println("Displaying values");
for(int i=0;iSystem.out.println(args[i]+" "+args[i].length());
}
}

java CmdLine nagpur 20 1.2 pune

output:
Count of values: 4
Displaying values
nagpur 6
20 2
1.2 3
pune 4


Length keyword of java returns count of strings. length() method of String class returns count of characters of given string.

new keyword



class A
{
int x;
}

class MyClass2
{
public static void main(String args[])
{
A obj1=new A();
A obj2=new A();
A obj3=obj2;

System.out.println(obj1);
System.out.println(obj2);
System.out.println(obj3);
}
}
Obj1 is class variable. New keyword creates instance of A class. Then constructor initializes members of that object. It stores 0 in numeric members and null in string members. Then new keyword returns initial address of that object and it is stored in class variable. Due to this, class variable behaves like pointer. In java, memory address is in hexa decimal.

Note: Java supports implicit pointers but not explicit pointers. Every class variable is pointer by default.

Demonstration of this keyword



class A
{
int x,y;
void getdata(int x1,int y1)
{
x=x1; y=y1;
}

void display()
{
System.out.println("x= "+x+", y= "+y);
}
}

class MyClass3
{
public static void main(String args[])
{
A obj1=new A();
obj1.getdata(2,4);
obj1.display();
}
}

output:

x=2 , y=4


class A
{
int x,y;
void getdata(int x,int y)
{
this.x=x; this.y=y;
}

void display()
{
System.out.println("x= "+x+", y= "+y);
}
}

class MyClass3
{
public static void main(String args[])
{
A obj1=new A();
obj1.getdata(2,4);
obj1.display();
}
}

Here this keyword points members of current instance. It is used when method local argument names and class member names are same.



Demonstration of empty constructors in inheritance



class A
{
A()
{
System.out.println("A class constructor");
}
}

class B extends A
{
B()
{
System.out.println("B class constructor");
}
}

class MyClass4
{
public static void main(String args[])
{
B obj1=new B();
}
}

Output:

A class constructor B class constructor

In inheritance, incase of empty constructors, child class constructor first executes parent class, constructor definition, then executes its own definition.

Demonstration of parameterized constructors with super method in
Inheritance



class A
{
int x;
A(int p)
{
System.out.println("A class constructor"); x=p;
}
}
class B extends A
{
int y;
B(int p,int q)
{
super(p);
y=q;
System.out.println("B class constructor");
}
void display()
{
System.out.println("x= "+x+", y= "+y);
}
}
class MyClass4
{
public static void main(String args[])
{
B obj1=new B(2,5);
obj1.display();
}
}

output:
A class constructor
B class constructor
x= 2, y= 5

In case of parameterized constructors in parent and child classes, super method calls parent class constructor. super() method should be in the first line in child class constructor.


Supar key word



class A
{
int x;
A(int p)
{
System.out.println("A class constructor");
x=p;
}
}

class B extends A
{
int x;
B(int p,int q)
{
super(p);
x=q;
System.out.println("B class constructor");
}

void display()
{
System.out.println("super.x= "+super.x+", x= "+x);
}
}

class CmdLine
{
public static void main(String args[])
{
B obj1=new B(2,5);
obj1.display();
}
}

Super keyword calls base class member, when base and derived class's member's names are same.


Keyboard Input



import java.io.DataInputStream;
import java.io.IOException;

class KeyInput
{
public static void main(String args[])throws IOException
{
DataInputStream dis=new
DataInputStream(System.in);
System.out.println("Enter name");
String str=dis.readLine();
System.out.println(str);
}
}

DataInputStream class object is created and initialized with System.in.readLine() method of DataInputStream class takes one line of string and returns.
readLine() may raise run-time exception called IOException. DataInputStream and IOException classes are in java.io package.

Wrapper classes



Java contains wrapper classes to convert data from one type to another.

Data type converts into Wrapper class

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

Integer class contains static method called parseInt().
It converts String type data to primitive int type.
Long class contains static method called parseLong().
It converts String type data to primitive long type.

Write a program to check whether entered number is even/odd.

import java.io.DataInputStream;
import java.io.IOException;
class KeyInput
{
public static void main(String args[])throws IOException
{
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter number");
String str= dis.readLine();
int n=Integer.parseInt(str);
if(n%2 == 0)
System.out.println("even");
else
System.out.println("odd");
}
}

To convert from String to primitive float, two steps are required.

1. Convert from String type to Float type
Float fobj=Float.valueOf(str);
Here valueOf is static method of Float class.
It takes String type of decimal value and converts to Float class object.

2. Convert from Float type to primitive float
float r=fobj.floatValue();
Here floatValue() method is nonstatic method of Float class. It converts Float type data to primitive float.

To convert from String to primitive double, two steps are required.


1. Convert from String type to Double type
Double dobj=Double.valueOf(str);
Here valueOf is static method of Double class. It takes String type decimal value and convert to Double class object.

2. Convert from Double type to primitive double
double r=dobj.doubleValue();
Here doubleValue() method is nonstatic method of Double class. It converts Double type data to primitive double.
import java.io.IOException;
import java.io.DataInputStream;
class KeyInput1
{
public static void main(String args[])throws IOException
{
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter radius");
String str=dis.readLine();
Float fobj=Float.valueOf(str);
float r=fobj.floatValue();
System.out.println("AREA OF CIRCLE IS :"+3.14*r*r);
}
}
Outputs:
5 *1 = 5
5 * 2 = 10
5 *3 = 15

1/1 -1/2 + 1/3 -1/4 +.... 1/n


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: