Sie sind auf Seite 1von 8

JAVA FOR BEGINEERS

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.

How Java is made platform independent?


For e.g. C and C++ compilers when installed in windows OS, it makes use of the
windows OS to run. So, you cannot take this compiled C and C++ code and run it on Linux. So,
this C/C++ are not platform independent.
In order to make java, a platform independent, they introduced another layer between java and
OS, which is called Java Virtual Machine (JVM). Now this JVM is dependent on the OS and not
java. Every operating system has its own JVM and so any java code can be taken and run on any
machine.

To program in Java, you need to:

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();
}
}

Commands to execute this program (HelloWorld.java):


% javac HelloWorld.java
% java HelloWorld

Program 1 Description:

The program named HelloWorld.java consists of a single class named HelloWorld.


It has a single method named main().
This method use two other methods: System.out.print() and System.out.println()
When you type java followed by a class name in your terminal application, the system
calls the main() method that you defined in that class, and executes its statements in
order, one by one.
Thus, typing java HelloWorld causes the system to call on the main() method in
PROGRAM 1 and execute its two statements.
The first statement calls on System.out.print() to print in the terminal window the
message between the quotation marks, and the second statement calls on
System.out.println() to terminate the line.

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.

public class tax_calc


{
public static void main(String[] args)

{
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)
{

System.out.println(Burger price is $ + a);


}
}
Exercise 5:
public class method_call_value
{
public static void main(String[] args)
{
double burger_price = 8.97;
double fries_price = 2.35;
Greetings();
price(burger_price,fries_price);
}
static void Greetings()
{
System.out.pirntln(Welcome to method_call);
}
static void price(double a, double b)
{
System.out.println(Burger price is $ + a);
System.out.println(Fries price is $ = + b);
}
}

The Scanner class next* methods


The Scanner class has a collection of next* methods that you can use to read a users
command-line input. Some of the most common methods are shown here:
next
finds and returns the next complete token from this scanner
next(pattern) returns the next token if it matches the specified pattern
nextBoolean returns true if the next token in this scanner's input can be interpreted
as a boolean value using a case insensitive pattern created from the string "true|false"
nextByte
scans the next token of the input as a byte
nextDouble scans the next token of the input as a double
nextFloat
scans the next token of the input as a float
nextInt
scans the next token of the input as an int
nextLine
advances this scanner past the current line and returns the input that was skipped
nextLong
scans the next token of the input as a long
nextShort
scans the next token of the input as a short

Exercise 6: (To find power and Rounding of value)


public class power
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
double a = in.nextDouble();
double b = in.nextDouble();
double result = (Math.pow(a,b));
System.out.println(result);
Result = Math.round(result);
System.out.println(result);
}
}

public class Math:


double abs(double a) absolute value of a
double max(double a, double b) maximum of a and b
double min(double a, double b) minimum of a and b
Note 1: abs(), max(), and min() are dened also for int, long, and oat.
double sin(double theta) sine function
double cos(double theta) cosine function
double tan(double theta) tangent function
Note 2: Angles are expressed in radians. Use toDegrees() and toRadians() to convert.
Note 3: Use asin(), acos(), and atan() for inverse functions.
double exp(double a) exponential (e a)
double log(double a) natural log (loge a, or ln a)
double pow(double a, double b) raise a to the bth power (ab )
long round(double a) round to the nearest integer
double random() random number in [0, 1)
double sqrt(double a) square root of a
double E value of e (constant)
double PI value of (constant)

Methods for Converting Strings to Primitive Types


int Integer.parseInt(String s) convert s to an int value
double Double.parseDouble(String s) convert s to a double value
long Long.parseLong(String s) convert s to a long value

Das könnte Ihnen auch gefallen