Java is a pure object-oriented programming language developed by the chief programmer of Sun Microsystems James Gosling and his team-mates for controlling consumer electronic devices. It shares many superficial similarities with C, C++, and Objective C (for instance for loops have the same syntax in all four languages); but it is not based on any of those languages, nor have efforts been made to make it compatible with them. Java has following important features:
1. Simple – Java is simple to learn. If you already have a bit knowledge of object oriented programming like C++ then it will be some easier to learn Java because Java inherits the
C/C++ syntax and many of the object-oriented features of C++.
2. Object-Oriented – Java is a pure object-oriented programming language. Java believes – everything is an object. It provides the features of abstraction, encapsulation, inheritance and
polymorphism. Any thing in Java is embedded into a class.
3. Compiled and Interpreted – Java source programs are translated into bytecode by java compiler and this bytecode is interpreted by the Java interpreter, thus Java is both compiled
and interpreted. When a Java source program is compiled, it lists all the errors occurred in a program, if any. And when this program is error free, it is interpreted by the interpreter
which executes it line by line. Only when the execution reaches with an error is the error reported. Of course it takes some extra time for debugging a program.
4. Portable – The bytecode, produced by a Java compiler, can be run on any platform that has Java interpreter. Thus java code is portable.
5. Robust – Java is a strictly typed language. It checks your code at compile time as well as run-time. Thus java programs are less error prone.
6. Multithreaded – Java supports multithreading programming environment, which allows you to write programs that do many things simultaneously. Even the core of Java is
multithreaded.
7. Secure – Java does not allow a programmer to manipulate the memory of a system.
8. Distributed – Java programs can access data across a network, therefore it is distributed.
9. Architecture Neutral and Portable – Java first converts a source program into bytecode which can be run on a variety of computers having different operating systems.
10. A High Performance Programming Language – Java programs are faster as compared to programs written in other interpreter-based programming languages.
11. Dynamic – Java provides an efficient way for maintaining different versions of an application.
/* IMPLEMENT SIMPLE JAVA PROGRAM */
class lohit
{
public static void main(String arr[])
{
System.out.println("Welcome To My Java World:Lohit");
}
}
OUTPUT :
/* IMPLEMENT PROGRAM OF JAVA USING CONSTRUCTOR */
class nesting
{
int m,n;
nesting(int x,int y)
{
n=x;
n=y;
}
int largest()
{
if(m>=n)
return(m);
else
return(n);
}
void display()
{
int large=largest();
System.out.println("largest value="+large);
} }
class nestingtest
{
public static void main(String args[])
{
nesting nest=new nesting(50,40);
nest.display();
}
}
OUTPUT:
/* IMPLEMENT PROGRAM OF JAVA USING INHERITANCE */
class room
{
int length,breadth;
room(int x,int y)
{
length=x;
breadth=y;
}
int area()
{
return(length*breadth);
} }
class broom extends room
{
int height;
broom(int x,int y,int z)
{
super(x,y);
height=z;
}
int volume()
{
return(length*breadth*height);
} }
class test
{
public static void main(String args[])
{
broom room1=new broom(15,10,20);
int area1=room1.area();
int volume1=room1.volume();
System.out.println("area1="+area1);
System.out.println("volume1="+volume1);
}
}
OUTPUT:
/* IMPLEMENT JAVA PROGRAM USING MULTITHREADING */
class A extends Thread
{
public void run()
{
for (int i=0; i<=2;i++)
{
System.out.println("\t From thread a"+i);
}}}
class B extends Thread
{
public void run(){
for (int j=0;j<=2;j++)
{
System.out.println("\t From thread b"+j);
}}}
class Thread1
{
public static void main(String args[])
{
new A().start();
new B().start();
}}
OUTPUT:
/* IMPLEMENT JAVA PROGRAM USING EXCEPTION HANDLING */
class Error4
{
public static void main(String args[])
{
int a[]={5,10};
int b=5;
try
{
int x=a[2]/b;
}
catch(ArithmeticException e)
{
System.out.println("Division by zero");
}
catch(Exception e)
{
System.out.println("ArrayIndexOutofBound");
}
finally
{
System.out.println("Finally executed");
}}}
OUTPUT:
/* IMPLEMENT JAVA PROGRAM USING INTERFACES */
interface area
{
final static float pi=3.14f;
float compute(float x, float y);
}
class rectangle implements area
{
public float compute(float x, float y)
{
return(x*y);
}
}
class circle implements area
{
public float compute(float x, float y)
{
return(pi*x*x);
}
}
class interfacetest
{
public static void main(String args[])
{
rectangle rect=new rectangle();
circle cir=new circle();
area are;
are=rect;
System.out.println("Area of rect="+are.compute(10,20));
are=cir;
System.out.println("Area of cir="+are.compute(10,0));
}
}
OUTPUT:
/* IMPLEMENT JAVA PROGRAM USING APPLETS */
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/**/
public class tools extends Applet implements ActionListener
{
String msg=" ";
Button b1,b2,b3;
TextField t1,t2,t3;
Label l1,l2,l3;
public void init()
{
b1=new Button("sum");
b2=new Button("multiply");
b3=new Button("divison");
t1=new TextField(10);
t2=new TextField(10);
t3=new TextField(10);
l1=new Label("enter ist no");
l2=new Label("enter 2nd no");
l3=new Label("result");
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(b1);
add(b2);
add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
t1.addActionListener(this);
t2.addActionListener(this);
t3.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String msg =ae.getActionCommand();
int x,y,z;
x=Integer.parseInt(t1.getText());
y=Integer.parseInt(t2.getText());
if(msg.equals("sum"))
{
z=x+y;
}
else if (msg.equals("multiply"))
{
z=x*y;
}
else
{
z=x/y;
}
t3.setText(" "+z);
repaint();
}
public void paint (Graphics g)
{
g.drawString(msg,20,20);
}
}
OUTPUT:
I started learning Java. But, until recently, I had no idea about the same. I didn't knew that something magical existed that would perform thisconversion. But now since I know a bit, I thought I better share it.
Java Decompiler
As you would have guessed by now, a Java Decompiler is a computer program capable of reversing the work done by a Compiler. In essence, it can convert back the Bytecode (the .class file) into the source code (the .java file).
There are many decompilers that exist today, but we will talk about the most widely used JD - Java Decompiler, which is available both as astand-alone GUI program and as an Eclipse-plugin.
To install and use these tools are a breeze and would not take you more than a few minutes to get accustomed to it. Hence, I would not repeat the process that's already mentioned on their site.One thing that we must note here is that the process of conversion mightNOT result into 100% exact code, i.e. the generated Java file might not match the actual Java code character by character. However, most of the code would be replicated but things like variable & function names or some other minor details may differ.L
ets have a look at the JD-GUI, stand-alone tool, written in C++;making it pretty fast to execute(decompile) and display the result. Also, it isindependent of the the Java Runtime Environment and thus no setup is required to install it.
Lets test the tool now.
Example Java Code:
public class test{public static void main(String[] args){System.out.println("Hello world");}}
Compile it: javac test.java
so that we have ByteCode (test.class file) with us now.
Decompile it using any one of the following ways:
Execute the following on the command line: jdi-gui.exe test.class
Select 'Open File' from the menu, browse to get to the test.class file and open it
Drag and Drop the test.class file into the JD-GUI tool
All of the above situations result in generating the following Java Code, have a look:
import java.io.PrintStream; public class test{public static void main(String[] paramArrayOfString){System.out.println("Hello world");}
Well just by seeing JD perform really well on this simple example,we cannot decide how efficient is this tool. But, still it is a tool thatevery Java Developer must be aware of. Because, in case you have accidently deletedyour Java files and are left with only the .class files, this is the tool that can save your neck.
Flow control in java is a key part as these are widely used in source code of any of the programs that are written in Java. In java, flow controls include if statement , switch statement, loops like for, while, do - while loop. These flow controls are described as following with it syntax and sample of source code.
These are oftenly called as decision statements. This is because whenever you use decision statements, you need to provide some mathematical expression that gets executed and decide which sort of action is to be taken.
if statement
Syntax for if statement is as follows:
if (expression) {
// any source code
}
In above syntax, expression is any mathematical expression that returns boolean value. Here, expression needs to be mandatorily boolean.
This expression can be anything like be "x" any variable whose value is to be compared with 1.
for example, if(x == 1) {
System.out.println("In if statement");
}
Above example shows that "x == 1" is checked and if value of "x" is "1" , then the expression will return "true" as value and then the control will move inside the if statement and print "In if statement" because "System.out.println()" is used to display any text on screen.
if statement also gets attached with else block. Its syntax would be
if (expression) {
// source code
} else {
// source code
}
In above syntax, if value of expression becomes "true" then only the control will enter into "if" block else control will enter into "else" block.
For example, if(x == 3) {
System.out.println("Value is 3.");
} else {
System.out.println("Value is not 3.");
}
In above example, if value of "x" is 3 then the control will enter inside "if" block and print "Value is 3." on screen and then control will not enter else block because "if" block has got executed. Similarly, if expression value has come out to be "false" then the control have directly entered into "else" block and would have printed "Value is not 3." on screen.
Furthermore, there is one more block which java provides and it is "else if " block. Its syntax would be something like this
if (expression1) {
// source code
} else if(expression2) {
// source code
} else {
// source code
}
In above syntax, if "expression1" gets satisfied then the control will enter into "if" block , execute the code written in it and come out of the block. Control will enter neither into "else if" block nor into "else" block.
If "expression1" is not satisfied, then the control will execute "else if" bloack and validate the expression. If "expression2" becomes true then the control will enter into "else if" block and execute the code written in "else if" block. But, if "expression1" and "expression2" are not satisfied too, then the control will enter directly into "else" block and execute the code written in it.
for example, if(x == 1) {
System.out.println("Value is 1.")
} else if( x == 2) {
System.out.println("Value is 2.")
} else {
System.out.println("Value is neither 1 nor 2.");
}
In above example, if "value" is 1 then "Value is 1." will get printed into screen. If value is 2 then "Value is 2." will get printed and if value is neither 1 nor 2, then "Value is neither 1 nor 2" will get printed or displayed.
There are following rules for using "else" and "else if" :
1: Zero or one "else" for a given "if" can be used and it must come after any else ifs.
2: We can have zero to many else ifs for a given "if" and they must come before the "else". "else" is optional.
3: If one "else if" gets executed then none of the remaining elses or else ifs will be tested.
Furthermore, two expressions can be given in one "if" statement.
Its syntax will be like this
if(expression1 && expression2) {
// Source code
}
Here, in above syntax, to make control of the program enter into "if" block, both "expression1" and "expression2" needs to be satisfied i.e. values of both expressions must be "true".
For example, consider value of x=1, y=2 then
if(x == 1 && y == 2) {
System.out.println("Values are 1 and 2.");
}
Here, in above example, if values of "x" and "y" are 1 and 2 respectively then only control of the program will enter into "if" block and print "Values are 1 and 2." on the screen. If either of the two values fails then control will not enter into "if" block and that's because of the use of logical AND operator " && " which demands both expressions to be mandatorily be "true" for execution.
We can use other operators as well like ! (NOT operator, which requires only one expression), !! (OR operator, which gets executed even if either of the expressions succeeds).
Syntax for using NOT operator will be something like this
if( !expression) {
// source code
}
Here, it will make the value of expression exactly the opposite i.e. if value of expression is "true" then by use of NOT (!) operator, value will get changed to "false" and vice-versa.
Similarly, with OR operator, its syntax will be
if(expression1 !! expression2) {
// source code
}
Here, if either "expression1" or "expression2" or both succeeds then the control will enter into "if" block but if neither of the expressions succeeds, "if" block will not get executed.
As the title suggests "Mobile operating system", Android is a java based mobile operating system with which mobile applications can be developed. Android strongly supports java. Any other programming language is not supportive to android. Cell phones with Android as operating system are not much in market. Cell phones of HTC comes with Android as their operating system. Recently, HTC mobile named as "Hero" was launched with operating system as Android. Android is an open source and very easy to install. Recently, 1.6 version of android sdk has been released.
All you need is to download SDK (Software Development Kit) which can be downloaded from developer.google.com/android.sdk/ . You also need to have Eclipse Ganymede or Eclipse Europa. SDK comes with virtual emulator which gets launched when any application is built in eclipse and ready for its execution or testing. Emulator gives exact look and feel of how your application will look on actual cell phone. Emulator, on its execution, gives UI of display sreen of a cell phone with num pad. When installed for first time, at the time of execution, virtual emulator takes time, may be 5 minutes or may be half an hour because it is running for the first time. APIs are also provided alongwith SDK.
Android is not in use much in indian market as most of cell phone companies use symbian as operating system for their sell phones. But sooner, android will be in demand and will occupy the market, for sure.
hi friends,
i myself found the solution for the java prog which i was unable to do, in my last article..
the solution is
the connecting function in the 2nd class..
public boolean UsrAuthPw(String username, String password){
boolean status = false;
try{
Connection con = getconnect();
Statement s = con.createStatement();
System.out.println("Username : "+ username +" Password : "+ password );
ResultSet rs = s.executeQuery("SELECT UserId, Password from usrauth where UserId = '"+ username +"' and Password = '"+ password +"'");
while (rs.next()){
status = true;
}
}
catch(java.sql.SQLException sqlexcp){
sqlexcp.printStackTrace();
System.out.println("SQL Exception");
status = false;
}
return status;
}
and the actionPerformed function of the 3rd class is,
public void actionPerformed(ActionEvent e){
String s1 = t1.getText();
String s2 = t2.getText();
if (s1.equals(s2)){
l3.setText("Both are same");
} else {
UsrConn uc = new UsrConn();
uc.getconnect();
boolean b = uc.UsrAuthPw(s1, s2);
if (b == true){
l3.setText("User logged in");
}
else{
l3.setText("Incorrect UserId or Password");
}
}