Presentation is loading. Please wait.

Presentation is loading. Please wait.

What Is Java? The name Java is a trademark of Sun Microsystems

Similar presentations


Presentation on theme: "What Is Java? The name Java is a trademark of Sun Microsystems"— Presentation transcript:

1 What Is Java? The name Java is a trademark of Sun Microsystems
developed by Sun and released in public alpha and beta versions in 1995. used to create executable content that can be distributed through networks. Now, the name Java refers to a set of software tools for creating and implementing executable content using the Java programming language.

2 The goal of Java’s development team was to develop consumer electronic products that could be simple and bug-free. What was needed was a way to createplatform-independent code and thus allow the software to run on any CPU. to implement this platform-independence, the development team focused first on C++ many similarities with C++

3 JAVA - The Big Difference
Computers do not understand the languages (C++, Java, etc) that programs are written in. Programs must first be compiled (converted) into machine code that the computer can run. A compiler is a program that translates a programming language into machine code.

4 Multiple Compilers Because different operating systems (Windows, Macs, Unix) require different machine code, you must compile most programming languages separately for each platform. program compiler compiler compiler Unix Win MAC

5 Java Interpreter Java is a little different.
Java compiler produces bytecode not machine code. Bytecode can be run on any computer with the Java interpreter installed. Win Interpreter Java Program Java Bytecode MAC compiler Interpreter Interpreter Unix

6 Advantages and Disadvantages of Java
Java is platform independent. Once it's compiled, you can run the bytecode on any machine with a Java interpreter. You do not have to recompile for each platform. Java is safe. Certain common programming bugs and dangerous operations are prevented by the language and compiler. Eg. No pointers in Java!!!! Java standardizes many useful operations like managing network connections and providing graphical user interfaces. Disadvantages: Running bytecode through the interpreter is not as fast as running machine code, which is specific to that platform. Because it is platform independent, it is difficult to use platform specific features (e.g., Windows taskbar, quick launch) in Java. Java interpreter must be installed on the computer in order to run Java programs.

7 Your First Java Program
Open your text-editor and type the following piece of Java code exactly: class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } Save this file as HelloWorld.java (watch capitalization) in the following directory: d:\java

8 Compiling and Running Your First Program
Open the command prompt in Windows To run the program that you just wrote, type at the command prompt: cd d:\java Your command prompt should now look like this: d:\java> To compile the program that you wrote, you need to run the Java Development Tool Kit Compiler as follows: At the command prompt type: d:\java> javac HelloWorld.java You have now created your first compiled Java program named HelloWorld.class To run your first program, type the following at the command prompt: d:\java>java HelloWorld Although the file name includes the .class extension , this part of the name must be left off when running the program with the Java interpreter.

9 Variables and Primitive Data Types
Description byte Variables of this kind can have a value from: -128 to +127 and occupy 8 bits in memory short to and occupy 16 bits in memory int to and occupy 32 bits in memory long to and occupy 64 bits in memory float Variables of this kind can have a value from: 1.4e(-45) to 3.4e(+38) double 4.9e(-324) to 1.7e(+308) char Variables of this kind can have a value from: A single character boolean True or False

10 Same as C++

11 Strings Strings consist of a series of characters inside double quotation marks. Examples statements assign String variables: String coAuthor = "John Smith"; String password = "swordfish786"; Strings are not one of the primitive data types, although they are very commonly used.

12 Appendix I: Reserved Words
The following keywords are reserved in the Java language. They can never be used as identifiers: abstract assert boolean break byte case catch char class const continue default do double else extends final finally float for goto if implements import instanceof int interface long native new package private protected public return short static strictfp super switch synchronized this throw throws transient try void violate while

13 The 5 Operators Arithmetic operators Assignment operator
Increment/Decrement operators Relational operators Conditional operators

14 Same as C++

15 Using && and || Examples:
(a && (b++ > 3)) (x || y) Java will evaluate these expressions from left to right and so will evaluate a before (b++ > 3) x before y Java performs short-circuit evaluation: it evaluates && and || expressions from left to right and once it finds the result, it stops.

16 Short-Circuit Evaluations
(a && (b++ > 3)) What happens if a is false? Java will not evaluate the right-hand expression (b++ > 3) if the left-hand operator a is false, since the result is already determined in this case to be false. This means b will not be incremented! (x || y++) What happens if x is true? Similarly, Java will not evaluate the right-hand operator y if the left-hand operator x is true, since the result is already determined in this case to be true.

17 References Summary of Java operators Order of Operations (PEMDAS)
Order of Operations (PEMDAS) Parentheses Exponents Multiplication and Division from left to right Addition and Subtraction from left to right

18 Control Structures If……..Else Switch statements Loops

19 Same as C++

20 The array object is of type int
Arrays We are creating a new array object Specifies an array of variables of type int int[] primes = new int[10]; // An array of 10 integers The array object is of type int and has ten elements The name of the array Another Way to Construct Arrays String[ ] names = { “Dereje", “Aman", “Yishak", “Tamirat", “Ebissa"};

21 Length of array Output: 5 Another example
String[ ] names = { “Dereje", “Aman", “Yishak", “Tamirat", “Ebissa"}; int numberOfNames = names.length; System.out.println(numberOfNames); Output: 5 Another example for(int i = 0; i < names.length; i++){ System.out.println("Hello " + names[i] + "."); } Important: Arrays are always of the same size: their lengths cannot be changed once they are created!

22 Exercise Which of the following sequences of statements does not create a new array? a. int[] arr = new int[4]; b. int[] arr; arr = new int[4]; c. int[] arr = { 1, 2, 3, 4}; d. int[] arr; just declares an array variable

23 Exercise Given this code fragment, What is the output?
int[] data = new int[10]; System.out.println(data[4]); What is the output? 0 – no junk values like C++

24 Methods

25 Methods are also known as functions or procedures.
There are return type and void methods just like in C++. But there is no passing arguments in reference like in C++.

26 Static Methods Some methods have the keyword static before the return type: static double divide(double a, double b) { return a / b; } We'll learn what it means for a method to be static later For now, all the methods we write in lab will be static.

27 main - A Special Method The only method that we have used in lab up until this point is the main method. The main method is where a Java program always starts when you run a class file with the java command The main method is static has a strict signature which must be followed: public static void main(String[] args) { . . . }

28 main continued class SayHi { public static void main(String[] args) {
System.out.println("Hi, " + args[0]); } When java Program arg1 arg2 … argN is typed on the command line, anything after the name of the class file is automatically entered into the args array: java SayHi Aman In this example args[0] will contain the String “Aman", and the output of the program will be "Hi, Aman".

29 Recursive Example class Factorial { public static void main (String[] args) { int num = Integer.parseInt(args[0])); System.out.println(fact(num)); } static int fact(int n) { if (n <= 1) return 1; else return n * fact(n – 1); After compiling, if you type java Factorial 4 the program will print out 24

30 Summary Methods capture a piece of computation we wish to perform repeatedly into a single abstraction Methods in Java have 4 parts: return type, name, arguments, body. The return type and arguments may be either primitive data types or complex data types (Objects) main is a special Java method which the java interpreter looks for when you try to run a class file main has a strict signature that must be followed: public static void main(String args[])

31 Lab Exercise What’s the difference between System.out.println(name) and System.out.println("name")? Play with displaying different texts. Create a new Java class called TempConverter. Add a main method to TempConverter that declares and initializes a variable to store the temperature in Celsius. In the main method, compute the temperature in Fahrenheit. Modify the above programme so that it can convert the number it is given as an argument when running it, i.e. typing java TempConverter 40 should give the correct Fahrenheit value.

32 Exercise contd……… Create a new class called Gradebook. In the main method, declare and initialize an array of doubles to store the grades of a student. Add an option for the user to print all the grades, or to print the average, or to print the grade earned. We will give you the class EasyReader which enables you to get inputs from the user. Copy EasyReader.class to your working directory to get started. EasyReader has the following methods: readInt(), readWord(), readByte(), readShort(), readLong(), readDouble(), readFloat() and readChar(). For example, to read a double from the user, you could use the following code: double d = EasyReader.readDouble(); You’ll learn how to create such classes in no time!! There should be methods avgGrade, printGrades and earnedGrade that accept arguments to do the three jobs specified.

33 End of Day 1


Download ppt "What Is Java? The name Java is a trademark of Sun Microsystems"

Similar presentations


Ads by Google