410 likes | 733 Views
Java Basics. Compiling. A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from C or Assembly to machine code Java must be (explicitly) compiled before it is run
E N D
Compiling • A “compiler” is a program that translates from one language to another • Typically from easy-to-read to fast-to-run • e.g. from C or Assembly to machine code • Java must be (explicitly) compiled before it is run • The Java compiler turns Java source code (.java) into Java bytecode (.class)
The Java Platform • The Java Virtual Machine (JVM) is responsible for running bytecode • The idea: bytecode can be interpreted quickly • The same bytecode can be interpreted on any architecture: write once, run anywhere • Code (C,C++) compiled to machine code must be compiled to a specific system
The Java Language • Created by Sun Microsystems • Introduced in 1995, initial popularity grew due to Internet applications • Excitement surrounding Java applets • Confusion with Javascript • Steady rise in popularity has continued for “better” programming reasons
A Historical Interlude: The Java Team • Java originally intended to be used on “smart” consumer electronics • Bill Joy • Founded Sun, 1982 • Intelligent robots will replace humanity in the near future… • James Gosling (“the father of Java”) • University of Calgary grad • First JVM, compiler, interpreter • also developed Emacs • Patrick Naughton • Arrested in late 90s on child predator charges • Not mentioned so much as a founding father anymore
The Java Language (cont’d) • … is a high-level programming language • … is very object oriented • … is similar to C++ and C • … typically compiled to Java bytecode • … is often confused with the Java Platform, but these are two different aspects of “Java”
Syntax and Semantics • The syntax rules of a language define how we can combine reserved words, symbols, and identifiers • The semantics of a program statement define what the statement means • Problem with program syntax = “error” • Problem with program semantics = “bug”
Java Program Structure • A Java program consists of: • One or more classes • A class contains one or more methods • A method contains program statements • We will explore these terms in detail
Java Program Structure // comments about the class public class MyProgram { } class header class body Comments can be placed almost anywhere
Java Program Structure // comments about the class public class MyProgram { } // comments about the method public static void main (String[] args) { } method header method body
Hello World // HelloWorld.java public class HelloWorld { } public static void main (String[] args) { } System.out.println(“Hello World!”);
Hello World // HelloWorld.java • Creates a “class” called HelloWorld • Compiled to HelloWorld.class • Classes used to define objects… later public class HelloWorld { } public static void main (String[] args) { } System.out.println(“Hello World!”);
Hello World // HelloWorld.java • The “main” method is where it starts to run • Ignore “public static void” and “String[] args” for now public class HelloWorld { } public static void main (String[] args) { } System.out.println(“Hello World!”);
Hello World // HelloWorld.java • Contains one “statement” • The System.out.println function comes from the Java “class library” • Ends with a semicolon (all statements do) public class HelloWorld { } public static void main (String[] args) { } System.out.println(“Hello World!”);
Compiling and Running • Create the file HelloWorld.java in a text editor • Compile: • javac HelloWorld.java • Run: • java HelloWorld • Output: • Hello World!
Comments • Three kinds of comments: • To simplify: comments are good // a one-line comment /* a multi-line comment */ /** a javadoc comment */
Reserved Words and Identifiers • Reserved words are specified by the language • All Java reserved words are in the text • Identifiers are specified by a programmer • Maybe you: e.g. HelloWorld • Maybe someone else: e.g. println
Restrictions and Conventions • Restriction • Identifiers can not start with a digit • Conventions • Title case for class names: HelloWorld • Uppercase for constants: MAX
White Space Conventions • Idea: make programs easy to read • Use consistent indentation • Use blank lines and comments to visually separate methods • The fact that it compiles doesn’t make it right…
Strong Typing • Java is a “strongly typed” language • All variables and values have a specific type • Type is known when the program is compiled…. before it is run • So all variables and values must be declared with a type before being used
Declaring Variables • Syntax: • Examples: • int count1, int count 2; • int count = 0; • String course1 = “CMPT 126”; <variable declaration> ::= <type> <declarator>, …. ; <declarator> ::= <identifier> <declarator> ::= <identifier> = <expression>
Assignment • We use the = operator for variable assignment • Initialization is a special case • When a value is assigned, the old value is overwritten • In Java, we use the final modifier to declare a variable constant • final int MAX_HEIGHT = 6;
Primitive Data Types in Java • Four integer types: • byte, short, int, long • Two floating point types • float, double • One of them is for characters • char • One of them is for boolean values • boolean
Expressions and Assignment • An expression is a combination of one or more operators and operands • Arithmetic operators: +, -, *, /, % • Use the normal order of operations e.g. int exp = 2 * 5 +7; count = count + 1; count++; • Boolean operators: &&, ||
More Assignment Operators • x += y is equivalent to x = x + y • Also: • -= • *= • /= • %=
Data Conversion • Non-matching types can be converted • A widening conversion is automatic • e.g. from short to int • A narrowing conversion may lose information • e.g. from float to int • Three kinds of conversion: • Assignment • Promotion • Casting
Assignment Conversion final int dollars = 6; double money; money = dollars; • Only works for widening conversion
Promotion int count = 2; float mass = 18.342; mass = mass / count; • Passing count to an operator that expects floating point values
Casting float mass = 18.342; int roundedmass = (int) mass; • Casting works for widening and narrowing • In this example, decimal part is just lost • Note: this does not actually round
Object Types • The primitive types aren’t really enough • Java also allows object types, or classes • Typically capitalized • Object variables hold references to objects • The declaration only creates a reference • This is different from primitive types • Variables of primitive type hold a value
Example: String Objects • We have already seen one object type in Java: String • A String object is a list of characters e.g. “Hello world!” or “My name is Aaron” • Can be passed to print or println • Can be concatenated using the (+) operator e.g. “Hello world! ” + “My name is Aaron” “I can also append numbers, like “ + 2
Object Instances • We must create a new “instance” of an object to store something • Each object type has a constructor (more later) • Create instances using the reserved world new e.g. course = new String(“CMPT 126”); • This creates a new String in memory • It stores the characters “CMPT 126” • The assignment sets course to refer to this instance
References and Instances String course; course: new String(“CMPT 126”) CMPT 126 course = new String(“CMPT 126”); course: CMPT 126