1 / 33

Java Basics

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

Download Presentation

Java Basics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Basics

  2. 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)

  3. 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

  4. 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

  5. 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

  6. 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”

  7. 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”

  8. 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

  9. Java Program Structure // comments about the class public class MyProgram { } class header class body Comments can be placed almost anywhere

  10. Java Program Structure // comments about the class public class MyProgram { } // comments about the method public static void main (String[] args) { } method header method body

  11. Hello World // HelloWorld.java public class HelloWorld { } public static void main (String[] args) { } System.out.println(“Hello World!”);

  12. 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!”);

  13. 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!”);

  14. 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!”);

  15. Compiling and Running • Create the file HelloWorld.java in a text editor • Compile: • javac HelloWorld.java • Run: • java HelloWorld • Output: • Hello World!

  16. Comments • Three kinds of comments: • To simplify: comments are good // a one-line comment /* a multi-line comment */ /** a javadoc comment */

  17. 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

  18. Restrictions and Conventions • Restriction • Identifiers can not start with a digit • Conventions • Title case for class names: HelloWorld • Uppercase for constants: MAX

  19. 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…

  20. 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

  21. 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>

  22. 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;

  23. 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

  24. 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: &&, ||

  25. More Assignment Operators • x += y is equivalent to x = x + y • Also: • -= • *= • /= • %=

  26. 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

  27. Assignment Conversion final int dollars = 6; double money; money = dollars; • Only works for widening conversion

  28. Promotion int count = 2; float mass = 18.342; mass = mass / count; • Passing count to an operator that expects floating point values

  29. 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

  30. 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

  31. 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

  32. 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

  33. References and Instances String course; course: new String(“CMPT 126”) CMPT 126 course = new String(“CMPT 126”); course: CMPT 126

More Related