Вы находитесь на странице: 1из 33



|


@ 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 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

ë
 



@ 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

Ñ
     

@ 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

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´

A
    

@ The „  „ of a language define how


we can combine reserved words, symbols,
and identifiers
@ The „
 „ of a program statement define
what the statement means
Problem with program syntax = ³error´
Problem with program semantics = ³bug´

o
 
  

@ A Java program consists of:


One or more „„ „
A class contains one or more
 „
A method contains 
„
„
@ Ëe will explore these terms in detail

·
 
  
// comments about the class
public class MyProgram
{
à 

à  

 à  à 



}

-
 
  
// comments about the class
public class MyProgram
{
// comments about the method
public static void main (String[] args)
{
 
  

|

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

||

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

@ Creates a ³class´ called HelloËorld


Compiled to HelloËorld.class
Classes used to define objects« later



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

@ The ³main´ method is where it starts to run


Ignore ³public static void´ and ³String[] args´ for
now



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

@ Contains one ³statement´


The ÿ 
 function comes from
the Java ³class library´
Ends with a semicolon (all statements do)



  

@ Create the file HelloËorld.java in a text editor


@ Compile:
javac HelloËorld.java
@ Run:
java HelloËorld
@ Output:
Hello Ëorld!

|D
 

@ Three kinds of comments:


// a one-line comment
/* a multi-line
comment */
/** a javadoc comment */

@ To simplify: comments are good

|A
   

@ ] „  „ are specified by the


language
All Java reserved words are in the text
@    „ are specified by a programmer
Maybe you: e.g. HelloËorld
Maybe someone else: e.g. println

|o
        

@ Restriction
Identifiers can not start with a digit
@ Conventions
   „ for class names: HelloËorld
ü  „ for constants: MAX


      

@ 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«

|-

 

@ Java is a ³strongly typed´ language


@ All variables and values have a specific type
@ Type is known when the program is
compiled«. x   it is run
@ So all variables and values must be declared
with a type before being used

÷



@ Syntax:
w  
 w
 w 
 
w 
 w

w 
 w
 w

@ Examples:
int count1, int count 2;
int count = 0;
String course1 = ³CMPT 126´;

÷|



@ Ëe use the = operator for variable


„„ 

     is a special case
@ Ëhen a value is assigned, the old value is
overwritten
@ In Java, we use the ×
modifier to
declare a variable constant
×



÷÷
     

@ Four integer types:


x  
  
@ Two floating point types
×  x 
@ One of them is for characters
à
@ One of them is for boolean values
x 

֑
   


@ An  „„  is a combination of one or


more  „and  „
@ Arithmetic operators: +, -, *, /, %
Use the normal order of operations
e.g.
 !"#$
à à #%
à ##
@ Boolean operators: &&, ||

ք
 
  

@ # is equivalent to #


@ Also:
-=
*=
/=
%=

÷D
   

@ Non-matching types can be converted


@ A      „  is automatic
e.g. from   to

@ A    „  may lose information
e.g. from ×   to

@ Three kinds of conversion:
Assignment
Promotion
Casting

÷A

   

×

 
x   
   

@ Only works for widening conversion

÷o
 

à  
×   %&'( 
  )à 

@ Passing count to an operator that expects


floating point values

÷·
 

×   %&'( 

   *
+ 

@ Casting works for widening and narrowing


In this example, decimal part is just lost
Note: this does not actually round

÷-
  

@ The primitive types aren¶t really enough


@ Java also allows x types, or „„ „
Typically capitalized
@ Object variables hold     „ to objects
The declaration only creates a reference
@ This is different from primitive types
Variables of primitive type hold a value

ë
 
 

@ Ëe have already seen one object type in


Java: ÿ

@ A ÿ
object is a list of characters
e.g. ³Hello world!´ or ³My name is Aaron´
@ Can be passed to 
 or 

@ Can be concatenated using the (+) operator
e.g. ³Hello world! ´ + ³My name is Aaron´
³I can also append numbers, like ³ + 2

ë|
    

@ Ëe must create a new ³instance´ of an object


to store something
Each object type has a „  (more later)
Create instances using the reserved world ,
e.g. à  ,ÿ
*-./% 0+
@ This creates a new ÿ
 in memory
It stores the characters ³CMPT 126´
The assignment sets à  to    this
instance

ë÷
      

String course;
à 1

new String(³CMPT 126´)


./% 

course = new String(³CMPT 126´);


à 1 ./% 

ëë

Вам также может понравиться