Download Statements and expressions

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Statements and
expressions - java
ABCs of Programming

A java program is made up of classes and objects, which are made up of
methods and variables.

Methods are made up of statements and expressions – the smallest elements
of java programming.

Looking at the basics of Java code

Statement – simple command that causes something to happen

int weight – 225;

System.out.println (“Lincoln High School is cool!”);

song.duration = 230;

Statement that produces a value is an expression

The value produced is the return value

Statements are terminated with a semicolon ;
Statements - continued

In theory, more than one statement can be put on a line, best not to.

Statements are grouped using an opening brace { and a closing brace }

A group of statements between the characters is a block.
Variables and Data Types

Java has three kinds of variables

Instance variables – define an object’s attributes

Class Variables – define the attributes of an entire class of objects

Local Variables – only used inside a method

We will work with local variables first.

In java, you must create a variable by giving it a name and declaring its type.

The type is listed first, followed by the name. EXAMPLES:
int loanLength;
String message;
boolean gameOver;
Variables and data types

EXAMPLES:
int loanLength;
String message;
boolean gameOver;

int represents integers

String is an object that holds text

boolean is used for Boolean true/false values

Local variables can be declared at any place inside a method, BUT, they must
be declared before they can be used.

You can create several variables of the same type on the same line.

String city, street, state;
Variables - continued

Variables can be assigned a value when created by using an equal = sign
followed by the value.

You must give values to local variables before you use them in a program, or
the program won’t compile successfully.

NAMING VARIABLES –

Names must start with a letter, an underscore character _ or a dollar sign $

Variables CANNOT start with a number.

camelCase naming convention is best.
Comments

Comments are essential to good programming.

Comments help to organize and provide information for you and those
working with you

I require that all programs be properly commented – get used to it!!

Single line comment two slashes // Everything from // to end of line is a
comment

Multi-line comments begins with /* and ends with */. Everything between
them is a comment