Upload
earl-francis-valdehuesa
View
219
Download
0
Embed Size (px)
Citation preview
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
1/27
DATA TYPES, STATEMENTS,
AND EXPRESSIONS IN JAVAfundamentals of object-oriented programming
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
2/27
BASIC SYNTAX
CASE SENSITIVITY
CLASS NAME
PROGRAM FILE NAME METHOD NAMES
public static void main(String[] args)
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
3/27
BASIC SYNTAX
DEALING WITHKEYWORDS
a word that has a special meaning defined by the Java
programming language
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
4/27
BASIC SYNTAX
STATEMENTS
simply create variables that you can use to store data
DECLARATION STATEMENT
performs calculations change values of variables, call
methods, and create objects
EXPRESSION STATEMENT
determine the order that statements are executed
CONTROL FLOW STATEMENT
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
5/27
BASIC SYNTAX
WHITE SPACErefers to one or more consecutive space characters, tab
characters, or line breaks
COMMENTSa bit of text that provides explanations of your code
completely ignored by the compiler
END-OF-LINE COMMENT begins with the sequence // and ends at the end of the line
TRADITIONAL COMMENT begins with the sequence /* and ends with the sequence */ and can span
multiple lines
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
6/27
BASIC SYNTAX
IDENTIFIERSa word that you make up to a Java programming
element by name
ALL IDENTIFIERS SHOULD BEGIN WITH a letter
currency character
underscore
AFTER THE FIRST CHARACTER, IDENTIFIERS CANHAVE ANY COMBINATION OF CHARACTERS
A KEYWORD CANNOT BE USED AS AN IDENTIFIER
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
7/27
BASIC SYNTAX
CREATING AN OBJECTFROM A CLASS
className variableName = new ClassName();
NEW KEYWORD
used to create instances / new objects
THREE STEPS CREATING AN INSTANCE FROM A CLASS declaration
instantiation
initialization
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
8/27
VARIABLES
a variable thats declared within the body of a method
LOCAL VARIABLE
BASIC FORM OF A VARIABLE DECLARATION type identifier;
TYPES OF VARIABLES
a variable that any method in a class can access
CLASS VARIABLE
associated with the instances of classes
INSTANCE VARIABLE
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
9/27
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
10/27
BASIC DATA TYPES
TWO DATA TYPES INJAVA
predefined by the Java language and named by a keyword
PRIMITIVE DATA TYPE
reference variables are created using defined constructors
of the classes
REFERENCE / OBJECT DATA TYPE
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
11/27
INTEGER TYPES
BYTE DATA TYPE an 8-bit signed twos complement integer
-128 to 127
SHORT DATA TYPE
a 16-bit signed twos complement integer -32,768 to 32,767
INT DATA TYPE a 32-bit signed twos complement integer
-2,147,4838,648 to 2, 147,483,647
LONG DATA TYPE a 64-bit signed twos complement integer
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,808
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
12/27
FLOATING TYPE
FLOAT DATA TYPE a single-precision 32-bit IEEE 754 floating point
precision is only about 6 or 7 decimal digits
DOUBLE DATA TYPE
a double-precision 64-bit IEEE 754 floating point precision is about 15 decimal digits
EXPONENTIAL NOTATION
used by floating point numbers to store their values records two
numbers: a base value (mantissa), and an exponent
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
13/27
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
14/27
WRAPPER CLASSES
wraps a primitive value to behave like an object used to convert
primitive values to strings and vice versa
Primitive Type Wrapper Class
int Integer
short Short
long Long
byte Byte
float Float
double Double
char Character
boolean Boolean
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
15/27
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
16/27
STRING PRIMITIVES TO STRINGS
Java automatically converts primitive values to string values
explicit conversion from primitives to a string use toString() method of the
primitive types wrapper class
STRINGS TO PRIMITIVES
converting a string to a primitive type uses aparse method of theappropriate wrapper class
Wrapper Class Wrapper Class
Integer parseInt(String)
Short parseShort(String)
Long parseLong(String)
Byte parseByte(String)
Float parseFloat(String)
Double parseDouble(String)
Character {none}
Boolean parseBoolean(String)
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
17/27
LITERAL
a source code representation of a fixed value
INTEGER TYPES
byte can be expressed in decimal, hexadecimal, or octal number systems
CHARACTER AND STRING LITERALS character literals are specified by enclosing the character on a pair of apostrophe ()
String literals enclose a sequence of characters between a pair of quotation marks()
ESCAPE SEQUENCES let you create literals for characters (e.g. , , space, tab, newline, etc) that cant
otherwise be typed within a character or String literal
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
18/27
MODIFIERS
keywords that you add to those definitions to change their meaning
TYPES OF MODIFIERS
ACCESS MODIFIERS set access levels for classes, variables, methods, and constructors
access levels are: default,private,public, andprotected
NON-ACCESS MODIFIERS used to achieve many other functionality
static,final, abstract, synchronized, andvolatile
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
19/27
OPERATORS
a special symbol or keyword used to designate a mathematical or some other type ofoperation that can be performed on one or more values, called operands
ARITHMETIC OPERATORS (+ - * / % ++ --)
BASIC OPERATORS
RELATIONAL OPERATORS (== != > < >=
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
20/27
OPERATORS
PRECEDENCE OF OPERATORSCategory Operator Associativity
Postfix () [] . (dot operator) Left to right
Unary ++ -- ! ~ Right to left
Multiplicative * / % Left to right
Additive + - Left to right
Relational > >= <
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
21/27
CASTING
a way to convert a variable from one data type to another data type
TWO TYPES OF CASTING PRIMITIVE DATA TYPE CASTING
implicit casting assigned to a data type of higher size (widening conversion)
explicit casting a data type of higher size cannot be assigned to a data type of
lower size (narrowing conversion)
REFERENCE TYPE CASTING
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
22/27
SELECTION CONTROLIF STATEMENT
lets you execute a single statement or a block of statements only if
a boolean expression evaluates to true
General form: if(boolean expression)
statement
IF-ELSE STATEMENT
adds an additional element to a basic if statement: a statement or
block thats executed if the boolean expression is not true
General form: if(boolean expression)
statement
else
statement
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
23/27
SELECTION CONTROL
ELSE IF STATEMENTa common pattern for nested if statements statement in
the else part happens to be another if statement
General form: if(boolean expression 1)
statement 1
else if(boolean expression 2)
satement 2
else if(boolean expression 3)
satement 3
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
24/27
SELECTION CONTROL
SWITCH STATEMENTallows a variable to be tested for equality against a list of
values each value is a case
General form: switch (boolean expression) {
case value1:statement
break;
case value2:statement
default:
}
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
25/27
ITERATION CONTROL
WHILE LOOPa type of loop that executes continuously as long as some
conditional expression evaluates to true
General form: while (boolean expression)
statement
DO LOOP
similar to a while loop, but with a critical difference: a do
loop is guaranteed to execute at least once
General form: do {
statement
}while (boolean expression);
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
26/27
ITERATION CONTROL
FOR LOOPmaintains a counter variable that is, a variable whose
value is increased time the body of the loop is executed
General form: for(initialization expression; test expression; count expression)statement
BREAK KEYWORD used to stop the entire loop
stop the execution of innermost and execute next line of code after the block
CONTINUE KEYWORD causes the loop to immediately jump to the next iteration of the loop
8/9/2019 2 - Data Types, Statements, And Expressions in Java FS 2014-2015
27/27
ARRAY
a data structure which stores a fixed-size sequential
collection of elements of the same type
DECLARING ARRAY VARIABLESdataType[]arrayReferenceVariable ordataType arrayReferenceVariable[]
CREATING ARRAYSarrayReferenceVariable =new dataType[arraySize];
dataType[]arrayReferenceVariable = {value0,value1,value2, ,valuen};