Beruflich Dokumente
Kultur Dokumente
Programming in Java:
Introduction:
Java is the programming language and was introduced by Sun Microsystems in 1995 and
instantly created a new sense of the interactive possibilities of the Web. Both of the major Web
browsers include a Java virtual machine. Almost all major operating system developers (IBM,
Microsoft, and others) have added Java compilers as part of their product offerings.
The Java Virtual Machine (JVM) is only one aspect of Java software that is involved in web
interaction. The Java Virtual Machine is built right into Java software download, and helps run
Java applications.
The "JDK" is the Java Development Kit. I.e., the JDK is bundle of software that you can use to
develop Java based software.
The "JRE" is the Java Runtime Environment. I.e., the JRE is an implementation of the Java
Virtual Machine which actually executes Java programs.
Typically, each JDK contains one (or more) JRE's along with the various development tools like
the Java source compilers, bundling and deployment tools, debuggers, development libraries, etc.
The Java virtual machine includes an optional just-in-time compiler that dynamically compiles
bytecode into executable code as an alternative to interpreting one bytecode instruction at a time.
In many cases, the dynamic JIT compilation is faster than the virtual machine interpretation.
JavaScript should not be confused with Java. JavaScript, which originated at Netscape, is
interpreted at a higher level, is easier to learn than Java, but lacks some of the portability of Java
and the speed of bytecode. Because Java applets will run on almost any operating system without
requiring recompilation and because Java has no operating system-unique extensions or
variations, Java is generally regarded as the most strategic language in which to develop
applications for the Web. (However, JavaScript can be useful for very small applications that run
on the Web client or server.)
Why JAVA?
Unlike other languages, java is platform independent. I.e., java can be run on any
machines with different OS.
Create a program by typing it into a file named, say, MyCode.java. (Writing a code).
Compile it by typing javac MyCode.java in a terminal window. (Compiles the code. I.e.,
translates the code into a form which is more suitable for the computer. The compiler
takes a file with a .java extension as input (your program) and produces a file with the
same name but with a .class extension).
Run (or execute) it by typing java MyCode in the terminal window. (Returns or transfers
the control of the system to your program. A part of the Java system known as the Java
Virtual Machine (the JVM, for short) directs your computer to follow your instructions.
To use the JVM to execute your program, type the java command followed by the
program name in a terminal window).
PROGRAM 1
public class HelloWorld
{
public static void main(String[]
args)
{
System.out.print("Hello, World");
System.out.println();
}
}
Program 1 Description:
Command Line
PROGRAM 2
public class cmdlinearg
{
public static void main(String
args[])
{
System.out.print(Hai, );
System.out.print(args[0]);
System.out.println(. How are
you?);
}
}
Argument
Program 2 Description:
% javac UseArgument.java
% java UseArgument Alice
Hi, Alice. How are you?
% java UseArgument Bob
Hi, Bob. How are you?
Exercise 1:
Write a program that prints the Hello, World message 10 times.
public class hello
{
public static void main(String[] args)
{
int no_of_repetitions = 10;
while(no_of_repetitions > 0)
{
System.out.println("Hello World");
no_of_repetitions--;
}
}
}
Data Types:
Exercise 2:
Write a program for the following description to calculate taxes.
{
double salary = 78678.65;
double tax = 0.0;
if(salary <= 15000){
tax = salary * .10;
}else if(salary <= 40000){
tax = salary * .20;
}else{
tax = salary * .30;
}
System.out.println("Tax = " + tax);
}
}
Method Call:
Exercise 3:
public class method_call
{
public static void main(String[] args)
{
Greetings();
}
static void Greetings()
{
System.out.pirntln(Welcome to method_call);
}
}
Exercise 4:
public class method_call_value
{
public static void main(String[] args)
{
double burger_price = 8.97;
Greetings();
price(burger_price);
}
static void Greetings()
{
System.out.pirntln(Welcome to method_call);
}
static void price(double a)
{