0% found this document useful (0 votes)
49 views35 pages

AP Lec1 Review

Java Programming Review - Advanced Programmin

Uploaded by

Bracika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views35 pages

AP Lec1 Review

Java Programming Review - Advanced Programmin

Uploaded by

Bracika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Advanced Programming

Review

Objects and Methods


Java is an object-oriented programming (OOP)
language
Programming methodology that views a program
as consisting of objects that interact with one
another by means of actions (called methods)
Objects of the same kind are said to have the
same type or be in the same class

Java Application Programs


A Java application program or "regular" Java program
is a class with a method named main
When a Java application program is run, the run-time
system automatically invokes the method named main
All Java application programs start with the main method

A Sample Java Application Program

Identifiers
Identifier: The name of a variable or other item
(class, method, object, etc.) defined in a program
A Java identifier must not start with a digit, and all the
characters must be letters, digits, or the underscore
symbol
Java identifiers can theoretically be of any length
Java is a case-sensitive language: Rate, rate, and RATE
are the names of three different variables

Identifiers
Keywords and Reserved words: Identifiers that have
a predefined meaning in Java
Do not use them to name anything else
public
class
void

static

Naming Conventions
Start the names of variables and methods with a
lowercase letter, indicate "word" boundaries with an
uppercase letter, and restrict the remaining
characters to digits and lowercase letters
topSpeed

bankRate1

timeOfArrival

Start the names of classes with an uppercase letter


and, otherwise, adhere to the rules above
FirstProgram

MyClass

String

Variable Declarations
Every variable in a Java program must be declared before it is
used
A variable declaration tells the compiler what kind of data (type) will
be stored in the variable
The type of the variable is followed by one or more variable names
separated by commas, and terminated with a semicolon
Variables are typically declared just before they are used or at the start
of a block (indicated by an opening brace { )
Basic types in Java are called primitive types
int numberOfBeans;
double oneWeight, totalWeight;

Primitive Types

Assignment Statements With Primitive Types


When an assignment statement is executed, the
expression is first evaluated, and then the variable on the
left-hand side of the equal sign is set equal to the value of
the expression
distance = rate * time;
Note that a variable can occur on both sides of the
assignment operator
count = count + 2;
The assignment operator is automatically executed from
right-to-left, so assignment statements can be chained
number2 = number1 = 3;

Shorthand Assignment Statements


Example:

Equivalent To:

count += 2;

count = count + 2;

sum -= discount;

sum = sum discount;

bonus *= 2;

bonus = bonus * 2;

time /=
rushFactor;
change %= 100;

time =
time / rushFactor;
change = change % 100;

amount *=
count1 + count2;

amount = amount *
(count1 + count2);

Assignment Compatibility
In general, the value of one type cannot be stored in
a variable of another type
int intVariable = 2.99; //Illegal
The above example results in a type mismatch because a
double value cannot be stored in an int variable

However, there are exceptions to this


double doubleVariable = 2;
For example, an int value can be stored in a double
type

Arithmetic Operators and Expressions


As in most languages, expressions can be
formed in Java using variables, constants, and
arithmetic operators
These operators are + (addition), - (subtraction),
* (multiplication), / (division), and % (modulo,
remainder)
An expression can be used anyplace it is legal to
use a value of the type produced by the
expression

Arithmetic Operators and Expressions


If an arithmetic operator is combined with int operands,
then the resulting type is int
If an arithmetic operator is combined with one or two
double operands, then the resulting type is double
If different types are combined in an expression, then the
resulting type is the right-most type on the following list that
is found within the expression
byteshortintlongfloatdouble
Exception: If the type produced should be byte or short (according
to the rules above), then the type produced will actually be an int

Precedence Rules

Integer and Floating-Point Division


When one or both operands are a floating-point type, division
results in a floating-point type
15.0/2 evaluates to 7.5

When both operands are integer types, division results in an


integer type
Any fractional part is discarded
The number is not rounded
15/2 evaluates to 7

Be careful to make at least one of the operands a floatingpoint type if the fractional portion is needed

Type Casting
A type cast takes a value of one type and produces a value of
another type with an "equivalent" value
If n and m are integers to be divided, and the fractional portion of the
result must be preserved, at least one of the two must be type cast to
a floating-point type before the division operation is performed
double ans = n / (double)m;
Note that the desired type is placed inside parentheses immediately in
front of the variable to be cast
Note also that the type and value of the variable to be cast does not
change

More Details About Type Casting


When type casting from a floating-point to an integer type,
the number is truncated, not rounded
(int)2.9 evaluates to 2, not 3

When the value of an integer type is assigned to a variable of


a floating-point type, Java performs an automatic type cast
called a type coercion
double d = 5;

Increment and Decrement Operators


The increment operator (++) adds one to the
value of a variable
If n is equal to 2, then n++ or ++n will change the
value of n to 3

The decrement operator (--) subtracts one


from the value of a variable
If n is equal to 4, then n-- or --n will change the
value of n to 3

Increment and Decrement Operators


When either operator precedes its variable, and is
part of an expression, then the expression is
evaluated using the changed value of the variable
If n is equal to 2, then 2*(++n) evaluates to 6

When either operator follows its variable, and is part


of an expression, then the expression is evaluated
using the original value of the variable, and only then
is the variable value changed
If n is equal to 2, then 2*(n++) evaluates to 4

The Class String


There is no primitive type for strings in Java
The class String is a predefined class in Java that is used to
store and process strings
Objects of type String are made up of strings of characters
that are written within double quotes
Any quoted string is a constant of type String
"Live long and prosper."

A variable of type String can be given the value of a


String object
String blessing = "Live long and prosper.";

Concatenation of Strings
Concatenation: Using the + operator on two strings in order
to connect them to form one longer string
If greeting is equal to "Hello ", and javaClass is equal to
"class", then greeting + javaClass is equal to "Hello
class"

Any number of strings can be concatenated together


When a string is combined with almost any other type of
item, the result is a string
"The answer is " + 42 evaluates to
"The answer is 42"

String Methods
The String class contains many useful methods for stringprocessing applications
A String method is called by writing a String object, a dot, the
name of the method, and a pair of parentheses to enclose any
arguments
If a String method returns a value, then it can be placed anywhere
that a value of its type can be used
String greeting = "Hello";
int count = greeting.length();
System.out.println("Length is " +
greeting.length());
Always count from zero when referring to the position or index of a
character in a string

String Indexes

Some Methods in the Class String (1)

Some Methods in the Class String (2)

Some Methods in the Class String (3)

Some Methods in the Class String (4)

Some Methods in the Class String (5)

Escape Sequences
A backslash (\) immediately preceding a
character (i.e., without any space) denotes an
escape sequence or an escape character
The character following the backslash does not
have its usual meaning
Although it is formed using two symbols, it is
regarded as a single character

Escape Sequences

String Processing
A String object in Java is considered to be immutable, i.e.,
the characters it contains cannot be changed
There is another class in Java called StringBuffer that has
methods for editing its string objects
However, it is possible to change the value of a String
variable by using an assignment statement
String name = "Soprano";
name = "Anthony " + name;

Naming Constants
Instead of using "anonymous" numbers in a program, always
declare them as named constants, and use their name instead
public static final int INCHES_PER_FOOT = 12;
public static final double RATE = 0.14;
This prevents a value from being changed inadvertently
It has the added advantage that when a value must be modified, it
need only be changed in one place
Note the naming convention for constants: Use all uppercase letters,
and designate word boundaries with an underscore character

Comments
A line comment begins with the symbols //, and
causes the compiler to ignore the remainder of the
line
This type of comment is used for the code writer or for a
programmer who modifies the code

A block comment begins with the symbol pair /*,


and ends with the symbol pair */
The compiler ignores anything in between
This type of comment can span several lines
This type of comment provides documentation for the
users of the program

Source
Absolute Java, Savitch
Copyright 2010 Pearson Addison-Wesley. All rights reserved.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy