Friday, 17 February 2012

overloading in java

In java method overloading means creating more than a single method with same name with different signatures.
For instance the + sign is used to add ints as well as
concatenate strings . The plus sign behaves differently
depending on the type of its arguments. Therefore the plus
sign is inherently overloaded.

Methods can be overloaded as well..same method with
different parameters is said to be method overloadingis one of the ways that java.----
we can perform the similar operation in different ways for
different parameters.

Constructors can be overloaded as well...Overloaded
Constructors provide multiple ways to initialize a new object

Method overloadingis one of the ways that java implements polymorphism.

In the example three methods are created with same name. java understands these methods with there signatures. java identifies the methods by comparing their signatures like return types, constructor parameters & access modifier used.



public class democlass {
public void add(int a , int b){
System.out.println("Result: "+a+b);
}
public void add(String a , String b){
System.out.println("Result: "+a+b);
}

public static void main(String[] args) {
democlass democlass=new democlass();
int a=10;
int b=20;
String temp1="Java";
String temp2="Source blog";
democlass.add(a, b);
democlass.add(temp1, temp2);

}

}


output

Result: 30
Result: JavaSource blog

Wednesday, 15 February 2012

interface in java

there are some conditions when it is important that whenever a particular entity is created it must have some methods . Means that methods are mandatory for that entity.
Like if we created a bike then it must have a break , race, petrol tank etc. these things are mandatory for a bike.

So if we say that a class must have some particular methods in it then we used interface.
interface is like a class similar to a class, that can contain only constants, method signatures, and nested types.
There are no method bodies. interface cannot be instantiated they can only be implemented by classes or extended by other interface.
which class impement the interface, all the methods which declared in interface will be define in that class .

Example of interface

interface is declared similer as class only interface keyword is used insted of class
no braces for methods and method is terminated by semicolon(;)

public interface TestInterface {
void frontBreak(String bikename);
void backBreak(String bikename);
}

//class implements interface
To use an interface, you write a class that implements the interface.
When class implements an interface, it provides a method body for each of the methods declared in the interface.

public class bikeClass implements TestInterface {

public void frontBreak(String bikename) {
//write method body here

}

public void backBreak(String bikename) {
//write method body here

}
}

Tuesday, 14 February 2012

Static

static variable

static variable belongs to the class and not to object/instance
static variables are initialized only once , at the start of the execution .

A single copy to be shared by all instances of the class
A static variable can be accessed directly by the class name and there is no need of object


variable declared as static are, essentially, global variable.
When object of its class are declared, no copy of a static variable is made.
Instead, all instances of the class share the same static variable.
Syntax of static variable : public static int test;



static Method

static Method belongs to the class and not to the object/instance.
A static method can access only static variable. It can not access non-static variable
A static method can call only other static methods and can not call a non-static method from it.
A static method can be accessed directly by the class name and there is no need of object

Syntax :public static void total()
A static method cannot refer to "this" or "super" keywords in anyway

Example of static variable

public class StaticClass {

public static int test;

public void print1(){
System.out.println("print1: "+test);
}
public void print2(){
System.out.println("print2: "+test);
}

public static void total(){
System.out.println("total: "+10+test);
}
}


we declared test as static variable and a static method in StaticClass

public class democlass {

public static void main(String[] args) {
StaticClass.test=4;
StaticClass a1=new StaticClass();
a1.print1();
StaticClass b1=new StaticClass();
b1.print2();
//call static method
StaticClass.total();

}

}

output

print1: 4
print2: 4
total: 104

Sunday, 12 February 2012

what is class

class

is a blue print for construct anything .
and user can crate that things by making the object of that class.
in real world ther are many objects of same kind. like a normal scooter is
an object of two wheeler class. and a bike is also an object of two wheeler class

the

class

has some properties and some methods.
we can use that methods by created an object of that class.

here is an example of class:
public class twowheelers{

int wheels=2;
int gears=4;
int seats;
int petrol;

public void changegear(int gears ){
this.gears=gears;
}
public void setPetrol(int petrol){
this.petrol=petrol;
}
public void changeSeat( int seats){
this.seats=seats;
}
public void showDetail(){
System.out.println("wheels="+wheels+ " gears:"+gears + " seats: "+seats +" petrol:"+petrol+"lt");

}
}

now we are creating an demo class in which we are crating two objects of twowheers class :


public class democlass {


public static void main(String[] args) {
twowheelers scooter=new twowheelers(); // first object
twowheelers bike = new twowheelers(); // second object

scooter.changegear(4);
scooter.changeSeat(2);
scooter.setPetrol(10);
scooter.showDetail();

bike.changegear(5);
bike.changeSeat(2);
bike.setPetrol(12);
bike.showDetail();
}
}


output:


wheels=2 gears:4 seats: 2 petrol:10lt
wheels=2 gears:5 seats: 2 petrol:12lt

Saturday, 11 February 2012

string comparison in java

import java.lang.*;
import java.io.*;

public class

CompareString

{
public static void main(String[] args) throws IOException{
System.out.println(

"comparison of string"

!);
BufferedReader bf =
new BufferedReader(new InputStreamReader(System.in));
System.out.println(

"Enter first string:"

);
String str1 = bf.readLine();
System.out.println(

"Enter second string:"

);
String str2 = bf.readLine();
if (str1.equals(str2)){
System.out.println(

"string is equals"

);
}
else{
System.out.println(

"string is not equals"

);
}
}
}

Out put of program

"comparison of string!"

"Enter first string:"

test

"Enter second string:"

test

"string is equals"



"comparison of string!"

"Enter first string:"

test

"Enter second string:"

Test

"string is not equals"

Wednesday, 8 February 2012

Send email in java


how to send email in java

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class

Mail

{

public static void main(String[] args) {
String[] to = { "example@example.com" };
String[] cc = { "example.example@example.com" };
String[] bcc = { "example@example.com" };
This is for google
Mail.sendMail("example@example.com", "example", to, cc, bcc,
"dont send spams",
"dont send spams..");
}

public synchronized static boolean sendMail(String userName, String passWord, String[] to,
String[] cc, String[] bcc, String subject, String text) {
String starttls = "true";
String auth = "true";
String port="465";
String host="smtp.gmail.com";
String socketFactoryClass="javax.net.ssl.SSLSocketFactory";
boolean debug=true;
String fallback="false";

Properties props = new Properties();
// Properties props=System.getProperties();
props.put("mail.smtp.user", userName);
props.put("mail.smtp.host", host);
if (!"".equals(port))
props.put("mail.smtp.port", port);
if (!"".equals(starttls))
props.put("mail.smtp.starttls.enable", starttls);
props.put("mail.smtp.auth", auth);
if (debug) {
props.put("mail.smtp.debug", "true");
} else {
props.put("mail.smtp.debug", "false");
}
if (!"".equals(port))
props.put("mail.smtp.socketFactory.port", port);
if (!"".equals(socketFactoryClass))
props.put("mail.smtp.socketFactory.class", socketFactoryClass);
if (!"".equals(fallback))
props.put("mail.smtp.socketFactory.fallback", fallback);

try {
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
msg.setText(text);
msg.setSubject(subject);
msg.setFrom(new InternetAddress("example@example.com"));
for (int i = 0; i < to.length; i++) {
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
to[i]));
}
if (cc != null) {
for (int i = 0; i < cc.length; i++) {
msg.addRecipient(Message.RecipientType.CC,
new InternetAddress(cc[i]));
}
}
if (bcc != null) {
for (int i = 0; i < bcc.length; i++) {
msg.addRecipient(Message.RecipientType.BCC,
new InternetAddress(bcc[i]));
}
}
msg.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, userName, passWord);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
return true;
} catch (Exception mex) {
mex.printStackTrace();
return false;
}
}

}
Related Posts Plugin for WordPress, Blogger...