general features of java programming language
DESCRIPTION
General Features of Java Programming Language. Variables and Data Types Operators Expressions Control Flow Statements. The Basic Demo Program. public class BasicsDemo { public static void main(String[] args) { int sum = 0; for (int current = 1; currentTRANSCRIPT
![Page 1: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/1.jpg)
General Features of Java Programming Language
Variables and Data Types
Operators
Expressions
Control Flow Statements
![Page 2: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/2.jpg)
The Basic Demo Program
public class BasicsDemo {
public static void main(String[] args) {
int sum = 0;
for (int current = 1; current <= 10; current++) {
sum += current; }
System.out.println("Sum = " + sum);
}
}
![Page 3: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/3.jpg)
The Count Class
import java.io.*;public class Count { public static void countChars(Reader in) throws IOException { int count = 0; while (in.read() != -1) count++; System.out.println("Counted " + count + " chars."); }}
Go Back: 6, 8, 17
![Page 4: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/4.jpg)
Running the countChars
• import java.io.*;public class Count {// ... countChars method omitted ...public static void main(String[] args) throws Exception{if (args.length >= 1) countChars(new FileReader(args[0]));else System.err.println("Usage: Count filename"); }}
![Page 5: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/5.jpg)
Variables and Data Types
Variables: Entities that act or are acted upon
Two Variables in Count: count and in
![Page 6: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/6.jpg)
Variable Declaration
• Variable Declaration:– Type of the variable– Name of the variable
• The location of the variable declaration determines its scope.
![Page 7: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/7.jpg)
Data Type
• Java is static-typed: All variables must have a type and have to be declared before use.
• A variable's data type determines its value and operation
• Two categories of data types in Java– primitive data type: byte, short, int, long, float....– reference data type: class, interface, array
count is ?
in is ?
Primitive
Reference
![Page 8: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/8.jpg)
Primitive and Reference Data Type
int x;x = 5;
x: 5
Point p1, p2;p1 = new Point();p2 = p1;
p1:
p2:
x: 0
y: 0Primitive Data
TypeReference Data Type
![Page 9: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/9.jpg)
Primitive Data Types
• byte 8-bit• short 16-bit• int 32-bit• long 64-bit• float 32-bit floating point• double 64-bit floating point• char 16-bit Unicode• boolean true/false
![Page 10: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/10.jpg)
Variable Names
• Java refers to a variable's value by its name.• General Rule
– Must be Legal Java identifier– Must not be a keyword or a boolean literal– Must not be the same name as another variable in the
same scope
• Convention:– Variable names begin with a lowercase letter
• isEmpty, isVisible, count, in– Class names begin with an uppercase letter
• Count
![Page 11: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/11.jpg)
Reserved Words(Keywords)
abstract default if private throwboolean do implements protected throwsbreak double import public transientbyte else instanceof return trycase extends int short voidcatch final interface static volatilechar finally long super whileclass float native switchconst* for new synchronizedcontinue goto* package this
Don't worry about what all these words mean or do, but be aware that you cannot use them for other purposes like variable names.
Don't worry about what all these words mean or do, but be aware that you cannot use them for other purposes like variable names.
![Page 12: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/12.jpg)
Variable Scope
• The block of code within which the variable is accessible and determines when the variable is created and destroyed.
• The location of the variable declaration within your program establishes its scope
• Variable Scope: – Member variable – Local variable – Method parameter – Exception-handler parameter
![Page 13: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/13.jpg)
Variable Scope
![Page 14: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/14.jpg)
Variable Initialization
• Local variables and member variables – can be initialized with an assignment statement when
they're declared.– The data type of both sides of the assignment
statement must match.• int count = 0;
• Method parameters and exception-handler parameters– cannot be initialized in the same way as local/member
vars– The value for a parameter is set by the caller.
![Page 15: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/15.jpg)
Final Variables
• The value of a final variable cannot change after it has been initialized.
• You can view final variables as constants.• Declaration and Initialization
– final int aFinalVar = 0;– final int blankfinal;
. . . blankfinal = 0;
![Page 16: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/16.jpg)
Literals (I)
• To represent the primitive types• Integer
– Decimal Value– Hexadecimal Value: 0x... (0x1f = 31)– Octal Value: 0... (076=62)
• Floating Point– 3.1415– 6.1D2 (64-bit Double; Default)– 3.4F3 (32-bit Float)
![Page 17: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/17.jpg)
Literals (II)
• Characters– ' ..... ‘ e.g. ‘a’– '\t', '\n' (Escape Sequence)
• Strings– ".......“ e.g. “Hello World!”– String Class (Not based on a primitive data type)
![Page 18: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/18.jpg)
Operators
Operators perform some function on operands.
An operator also returns a value.
![Page 19: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/19.jpg)
Operators (I)
• Arithmetic Operators– Binary: +, -, *, /, %– Unary: +, -, op++, ++op, op--, --op
• Relational Operators– >, >=, <, <=, ==, != (return true and false)
• Conditional Operators– &&(AND), ||(OR), !(NOT), &(AND), |(OR)– expression ? op1 : op2
• Bitwise Operators– >>, <<, >>>, &, |,^,~
(i>5) ? j=1 : j=2
if (i>5) j=1;else j=2;
![Page 20: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/20.jpg)
Operators (II)
• Assignment Operators– =– += – -=, *=, /=, %=, &=, !=, ^=, <<=, >>=, >>>=
op1 += op2
s += 2
op1 = op1 + op2
s = s + 2
![Page 21: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/21.jpg)
Expressions
Perform the work of a Java ProgramPerform the computation
Return the result of the computation
![Page 22: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/22.jpg)
Expression
• An expression is a series of variables, operators, and method calls that evaluates to a single value. – count ++– in.read() != -1
• Precedence– Precedence Table– Use (.....)
• Equal precedence – Assignment: Right to Left (a = b =c)– Other Binary Operators: Left to Right
![Page 23: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/23.jpg)
Expressions and operators• An expression is a program fragment that evaluates
to a single value– double d = v + 9 * getSalary() % PI;
• Arithmetic operators– additive +, -, ++, --
– multiplicative *, / % (mod operator)
• Relational operators– equality == (NB)– inequality !=– greater than and less than >, >=, <, <=
![Page 24: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/24.jpg)
Control Flow Statement
![Page 25: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/25.jpg)
If Statements
• if (boolean) {/* ... */ }else if (boolean) {/* ... */ }else {/* ... */ }
• The expression in the test must return a boolean value– Zero('') can't be used to mean false, or non-zero("...")
to mean true
Statement Block
![Page 26: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/26.jpg)
Example
if (income < 20000){ System.out.println (“poor”);}else if (income < 40000){ System.out.println (“not so poor”);}else if (income < 60000){ System.out.println (“rich”);}else { System.out.println (“ very rich”);}
![Page 27: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/27.jpg)
Loops
• while (boolean expression) {/* ... */}
• do {/* ... */} while (boolean expression)
• for (expression; booleanExpression; expression) {/* ... */}
![Page 28: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/28.jpg)
Example
// count from 1 to 10int i = 1;while (i<=10) {
System.out.println (i);i= i+ 1;
}
![Page 29: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/29.jpg)
Example
// count from 1 to 10int i = 1;do {
System.out.println (i)i= i+ 1;
} while (i< 10);
![Page 30: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/30.jpg)
The for statement
• The for statement has the following syntax:
for (initialisation; condition; increment) statement;
Reserved word
The initialisation partis executed once beforethe loop begins
The statement isexecuted until thecondition becomes false
The increment part is executedat the end of each iteration
![Page 31: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/31.jpg)
Example
// count from 1 to 10for (int i = 0; i < 10; i++) System.out.println (i);
![Page 32: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/32.jpg)
Secret! A for Loopcan always be converted to
a while loop
i = 0;
while (i < 10)
{
System.out.println(i);
i++;i++;
}
for(i = 0; i < 10; i++i++)
{
System.out.println(i);
}
This will help you understand the sequence of operations of a for loop
![Page 33: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/33.jpg)
Switch
• switch (expression) {case Constant1:/* ... */break;case Constant2:/* ... */break;....default:/* ... */break;}
![Page 34: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/34.jpg)
Multiple Selections via switch Note the "optional" default case at the end of the switch statement. It is technically optional It is technically optional onlyonly in terms of syntax. in terms of syntax.
switch (number) { case 1:
System.out.println ("One");break;
case 2:System.out.println ("Two");break;
case 3:System.out.println ("Three");break;
default: System.out.println("Not 1, 2, or 3"); break; // Needed???
} // switch
For safety and good programming practice, always include a 'default' case.
This mightwork without
the defaultcase, but would be
poortechnique
![Page 35: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/35.jpg)
How many days?if (month == 4 || month == 6 ||
month == 9 || month == 11)
numdays = 30;
else if (month == 2)
{
numdays = 28;
if (leap)
numdays = 29;
}
else
numdays = 31;
![Page 36: General Features of Java Programming Language](https://reader035.vdocuments.mx/reader035/viewer/2022062407/56812b93550346895d8fb24b/html5/thumbnails/36.jpg)
Switchswitch (month)
{
case 4:
case 6:
case 9:
case 11:
numdays = 30;
break;
case 2:
numdays = 28;
if(leap)
numdays = 29;
break;
default: /* Good idea? */
numdays = 31;
}