30
Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode is run by the run- time interpreter – java – or at time in an applet with a wrapper consisting of .html code

Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

  • View
    232

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Where are we?

Programming in Java consists of creating source code in an editor.

Source code is compiled to bytecode by the Java compiler – javac

The bytecode is run by the run-time interpreter – java – or at time in an applet with a wrapper consisting of .html code

Page 2: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Source Code

Must be prepared according to the strict standards of the language and the compiler.

Grouped in files, each file contains a public class with a given name.

Name of the public class must match the part of the filename before the .java exactly, including upper/lower case

Page 3: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Class

Consists of class variables declaration and methods grouped between a starting { and ending }.

Method will have a header followed by a sequence of declarations and statements enclosed in braces.

Each declaration and statement ends with a semi-colon;

Page 4: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Sample Layout

public class FirstProgram

{

public static void main(String[] args)

{

…declarations and statements

}

}

Page 5: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Declarations

Type variable;

Type variable=value;

Type can be one of the built in types (byte,short,int,long,float,double,char,boolean) or a class such as String, Random.

Page 6: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Base Variables

For the base types space is allocated and the variable stores the value of the variable. E.g., int x=9. x ‘stands for’ a location which contains the value 9.

Value can be changed by statements. Variables can be designated as constants with

the keyword “final”. In this case they must be given a value in the declaration.

Page 7: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Constants

final int MAX_ROW=10;

Constants cannot be modified by statements.

Page 8: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Class Variables

String title; Title does not allocate space for a String,

instead it is a class reference. That is, title will be used to store the address of a string.

Objects are created using the new operator and various methods including constructors.

Page 9: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Creating Objects

String title, extendedTitle;

title = new String(“This is my title”);

extendedTitle=title.concat(“ Live with it.”);

Page 10: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Statements

Assignment statements Invoking class methods

System.out.println(title);

More complex statements (later);

Page 11: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Invoking Methods

We can use classes that our program “knows about” either because it is always included (such as String or System.out) as part of the java sdk, or because we have specifically imported it and made it available:

import cs1.Keyboard;

Page 12: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Class Methods

Some classes require an object, and the methods are applied to the object:

String title=new String(“my title”);

int lengthOfMyTitle;

lengthOfMyTitle=title.length();

Page 13: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Static Methods

Other classes consist only of static methods and don’t require an object. Static methods can be invoked using the class name:

double radius=5.0;

double area;

area = Math.PI * radius * radius;

Page 14: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

main

Our simple programs consist of a single static method main. Since it is static, it can be invoked without creating a class object.

Since it is named main it will automatically be invoked when the program is run

Since it’s reserved, the format must be followed exactly

public static void main(String[] args)

Page 15: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Main

Actually, the variable args can have any name, the other parts are fixed though.

We are free to create other methods in our class and use them. For the class containing main, these will usually be static methods (since there won’t be an object declared) and can be private or public.

Page 16: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Simple Statements.

Assignments: x = 17; Expressions: x = a + b; Class methods:

System.out.println(“Hello”);

String title=“Hello”;

String moreTitle = title.concat(“ World”);

Page 17: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Complex Statements

if (condition) statement; if (condition)

statement;

else

statement; while(condition)

statement;

Page 18: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Conditions

Expression that results in a boolean value (true or false).

Boolean operators ==, <, >,<=,>=,!

if (a < b)

System.out.println(“a is smaller”);

else

System.out.println(“b is smaller”);

Page 19: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Conditions - more

For comparing objects, we usually cannot use the operators. The class defining the object will usually supply the necessary methods for comparisons

if (title.equals(extendedTitle))

Page 20: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

How do we find out about classes?

Important ones like string are discussed in the text

Many are included in the SDK, and we find out about them from the documentation with the SDK or from textbooks.

We write our own, and provide the documentation as part of the process.

Page 21: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Using classes

Use the wheel or invent the wheel?• If you are using an existing class, you should

take the time to at least skim-read the documentation. Often you will find that a method exists to do what you want:

if(title.compareToIgnoreCase(extendedTitle) < 0)

Page 22: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Flow of execution

Statements are executed sequentially if nothing is done to alter the flow.

Some statements alter the flow. When a method is invoked, flow actually

transfers to the code contained in the method, but conceptually we ignore this and think of it happening immediately.

Page 23: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

if and it’s variations

If statements as well as if/else and switch statements in effect cause forward jumps to execute or skip code based on a condition or the value of an expression.

Page 24: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Loops

Loops cause blocks of code to be repeated a number of times based on an expression that is evaluated at the beginning or end of each iteration

while(condition) { …}

do { …} while;

Page 25: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

for

for loops exist because many loops follow the format:

Initialization statements

while(condition)

{

….

statements to update the condition variable(s)

}

Page 26: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

for(…;…;…)

for(initialization;condition;increment)

for(int i=0; i < 100; i++)

System.out.println(i);

Page 27: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Programs

One or more .java files, compiled to .class files

Begins at main May write other methods in the class

containing main, but they must be static, since no object is created.

May use other classes and methods.

Page 28: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

Applets

No main method Run in context of web browser (or

appletviewer) Begin by ‘overriding’ Applet methods,

typically paint.

Page 29: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

What to review

Self-test exercises – answers in book. Exercises from chap 1, 2, 3 Programs you have done or done in

class.

Page 30: Where are we? Programming in Java consists of creating source code in an editor. Source code is compiled to bytecode by the Java compiler – javac The bytecode

What to expect

Short answer such as the exercises. Multiple choice/true false/or short answer

• Terms

• Programming

What’s wrong with this program? At least one short complete program. Other program segments.