Sie sind auf Seite 1von 11

Data and

Expressions II
Outline
Java Data Types
Variables and Assignment
Expressions
Data Conversion
Interactive Programs
variables
A variable is a name for a
location in memory that holds a
value

variable declaration specifies the variable's name and its type of information

data type variable name


int total; Multiple variables
can be created in
int count, temp, result; one declaration

variable initialization variable should be given initial value before used else error

int num;
int sum = 0;
int base = 32, max = 149;

variable assignment Assignment statement changes the value of a variable.


Value must be consistent with the declared type

total = 55;
Example 1 : variables declaration

//********************************************************************
// PianoKeys.java Author: Lewis/Loftus
//
// Demonstrates the declaration, initialization, and use of an
// integer variable.
//********************************************************************

public class PianoKeys


{
//-----------------------------------------------------------------
// Prints the number of keys on a piano.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int keys = 88;
System.out.println ("A piano has " + keys + " keys.");
}
}
Output
A piano has 88 keys.
Example 2 : variables declaration

public class Assignments {

public static void main(String args []) {

int x, y; // declare int variables


float z = 3.414f; // declare and assign float
double w = 3.1415; // declare and assign double
boolean b = true; // declare and assign boolean
char grade = ‘A’; // declare and assign character
String str; // declare String variable
String msg = "bye"; // declare and assign String

}
}
Example 3 : variables assignment
//********************************************************************
// Geometry.java Author: Lewis/Loftus
// Demonstrates the use and effect of an assignment statement
//********************************************************************
public class Geometry
{
//-----------------------------------------------------------------
// Prints the number of sides of several geometric shapes.
//-----------------------------------------------------------------
public static void main (String[] args)
{
int sides = 7; // declaration with initialization
System.out.println ("A heptagon has " + sides + " sides.");

sides = 10; // assignment statement


System.out.println ("A decagon has " + sides + " sides.");

sides = 12;
System.out.println ("A dodecagon has " + sides + " sides.");
} Output
}
A heptagon has 7 sides.
A decagon has 10 sides.
a dodecagon has 12 sides.
Identifiers name of a class, variable,
constant and method

• Can be made up of letters, digits, and


the underscore (_) character
• Cannot start with a digit
Java rules of • Cannot use other symbols such as ? or %
identifiers • Spaces are not permitted inside
identifiers
• You cannot use reserved words
• They are case sensitive

• variable names start with a


conventions lowercase letter
of identifiers • class names start with an
uppercase letter
used • constant names are in all
uppercase letters
reserve words
abstract default if
boolean do implements
break double import
byte else instanceof
case extends int
catch final interface
char finally long
class float native
const for new
continue goto package

private throw
protected throws
public transient
return try
short void
static volatile
super while
native switch
new synchronized
Package this
constant an identifier that holds the same
value during its entire existence –
cannot be reassigned

Declaring Why use


constant constant ?

First, give meaningful label to


. value, e.g. MAX_LOAD = 250

final int MIN_HEIGHT = 69; Second facilitate program


maintenance. value need only
be set in one place

Third avoid inadvertent errors by


other programmers when
change value
scope
Scope rules
Example :
{
int b = 1;
• Variable/Constant must be }
declared inside a block (within
braces) System.out.println(b);
• Variable/Constant is visible only in
it's block and it's child sections
Will not compile…
• We can have two variables with
similar name if each of them are
declared in different blocks
exercises
1.Create a small program that defines some fields. Try creating some
illegal field names and see what kind of error the compiler
produces. Use the naming rules and conventions as a guide.

2.In the program you created in Exercise 1, try leaving the fields
uninitialized and print out their values. Try the same with a local
variable and see what kind of compiler errors you can produce.
Becoming familiar with common compiler errors will make it easier to
recognize bugs in your code.

Das könnte Ihnen auch gefallen