You are on page 1of 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.)

class extension).. How Java is made platform independent? For e. So.out. you need to:    Create a program by typing it into a file named.java in a terminal window. (Compiles the code.Why JAVA? Unlike other languages. (Writing a code). they introduced another layer between java and OS. Now this JVM is dependent on the OS and not java. To use the JVM to execute your program.print("Hello. To program in Java. Compile it by typing javac MyCode. Every operating system has its own JVM and so any java code can be taken and run on any machine. World"). a platform independent. java can be run on any machines with different OS. A part of the Java system known as the Java Virtual Machine (the JVM. PROGRAM 1 public class HelloWorld { public static void main(String[] args) { System. In order to make java.g. (Returns or transfers the control of the system to your program. The compiler takes a file with a .java extension as input (your program) and produces a file with the same name but with a .. type the java command followed by the program name in a terminal window). I. java is platform independent. which is called Java Virtual Machine (JVM). C and C++ compilers when installed in windows OS.out. this C/C++ are not platform independent. Run (or execute) it by typing java MyCode in the terminal window.println().e. translates the code into a form which is more suitable for the computer. you cannot take this compiled C and C++ code and run it on Linux. for short) directs your computer to follow your instructions. So. it makes use of the windows OS to run. } } . I.e. say.java. MyCode. System.

print() to print in the terminal window the message between the quotation marks.Commands to execute this program (HelloWorld. This method use two other methods: System. typing java HelloWorld causes the system to call on the main() method in PROGRAM 1 and execute its two statements. System. How are you?”). and the second statement calls on System. The first statement calls on System.out.println(“. and executes its statements in order. } } Argument . the system calls the main() method that you defined in that class. “).out.print(args[0]).out. Thus.out.print(“Hai. It has a single method named main().out.out.print() and System. System.println() to terminate the line.java): % javac HelloWorld. Command Line PROGRAM 2 public class cmdlinearg { public static void main(String args[]) { System. one by one.out.java consists of a single class named HelloWorld.java % java HelloWorld Program 1 Description:       The program named HelloWorld.println() When you type java followed by a class name in your terminal application.

java % java UseArgument Alice Hi. Bob. no_of_repetitions--. Alice.println("Hello World").Program 2 Description: % javac UseArgument. public class hello { public static void main(String[] args) { int no_of_repetitions = 10. while(no_of_repetitions > 0) { System. How are you? % java UseArgument Bob Hi. } } } Data Types: . World message 10 times.out. How are you? Exercise 1: Write a program that prints the Hello.

public class tax_calc { public static void main(String[] args) .Exercise 2: Write a program for the following description to calculate taxes.

20. if(salary <= 15000){ tax = salary * . }else if(salary <= 40000){ tax = salary * .10. }else{ tax = salary * . } } Exercise 4: public class method_call_value { public static void main(String[] args) { double burger_price = 8. } static void price(double a) { .0.println("Tax = " + tax). } static void Greetings() { System.65.pirntln(“Welcome to method_call”).30.out. } } Method Call: Exercise 3: public class method_call { public static void main(String[] args) { Greetings().pirntln(“Welcome to method_call”). } System.{ double salary = 78678.97.out. } static void Greetings() { System. double tax = 0.out. Greetings(). price(burger_price).

} } 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.out. } static void price(double a.35.97.println(“Fries price is $” = + b). double b) { System.out. Greetings(). price(burger_price.println(“Burger price is $ “ + a).out.println(“Burger price is $ “ + a).pirntln(“Welcome to method_call”). System. double fries_price = 2. } } Exercise 5: public class method_call_value { public static void main(String[] args) { double burger_price = 8.out. } static void Greetings() { System.fries_price). 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 .System.

System. Result = Math. and atan() for inverse functions. acos(). Note 3: Use asin(). or ln a) double pow(double a. max(). double b) maximum of a and b double min(double a. Use toDegrees() and toRadians() to convert.out.round(result). double exp(double a) exponential (e a) double log(double a) natural log (loge a.Exercise 6: (To find power and Rounding of value) public class power { public static void main(String[] args) { Scanner in = new Scanner(System. double b) raise a to the bth power (ab ) long round(double a) round to the nearest integer double random() random number in [0.println(result). and min() are defined also for int.println(result). 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.in).out. double b = in. System.pow(a.parseDouble(String s) convert s to a double value long Long. and float.b)). double b) minimum of a and b Note 1: abs().parseInt(String s) convert s to an int value double Double. } } public class Math: double abs(double a) absolute value of a double max(double a. long. double a = in.nextDouble().parseLong(String s) convert s to a long value . double result = (Math. 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.nextDouble().