Java Keywords Reserved Words Part of the Java
- Slides: 37
Java Keywords Reserved Words Part of the Java language Examples: public, static, void Pre-defined Java Identifiers Defined in Java Libraries Examples: print & println User-defined Identifiers Examples: shown in this chapter
// Var 01. java // This program demonstrates how to declare integer variables with <int>, // and it shows how to display the value of a variable with <println>. public class Var 01 { public static void main (String[] args) { int a; int b; a = 10; b = 25; System. out. println(); System. out. println(a); System. out. println(b); System. out. println(); } } a 10 b 25 10 25
// Var 02. java // This program is the same as Var 01. java without assigning values // to the variables. Java does not compile a program that attempts to use // unassigned "simple" data types. public class Var 02 { public static void main (String[] args) { int a; int b; System. out. println(a); System. out. println(b); } } a ? b ?
// Var 03. java // This program demonstrates that it is possible to declare a variable // identifier and initialize the variable in the same statement. // It is a good habit to initialize variables where they are declared. public class Var 03 { public static void main (String[] args) { int a = 10; int b = 25; a 10 System. out. println(); System. out. println(a); System. out. println(b); System. out. println(); } } b 25 10 25
// Var 04. java // This program combines output of literals and variables. // "a: " is a string literal, which displays the characters a: // a is an integer variable, which displays its integer value 10. public class Var 04 { public static void main (String[] args) { int a = 10; int b = 25; a 10 System. out. println("a: " + a); System. out. println("b: " + b); } } a: 10 b: 25 b 25
Java Integer Data Types Data Type Bytes used in memory byte 1 short 2 int 4 long 8 Minimum Value Maximum Value -128 127 -32, 768 32, 767 -2, 147, 483, 648 2, 147, 483, 647 -9, 223, 372, 036, 854, 775, 808 9, 223, 372, 036, 854, 775, 807
// Numeric 01. java // This program demonstrates the five integer operations. public class Numeric 01 { public static void main (String[] args) { int a = 0; int b = 25; int c = 10; a = b + c; 25 25 25 + – * / % 10 10 10 = = = 35 15 250 2 5 // Addition System. out. println(b + " + c + " = " + a); a = b – c; // Subtraction System. out. println(b + " - " + c + " = " + a); a = b * c; // Multiplication System. out. println(b + " * " + c + " = " + a); a = b / c; // Integer Quotient Division System. out. println(b + " / " + c + " = " + a); a = b % c; System. out. println(b + " % " + c + " = " + a); } } // Integer Remainder Division
Integer Quotient Division Examples 12 12 / / / / 1 2 3 4 5 6 7 = = = = 12 6 4 3 2 2 1 12 / 8 12 / 9 12 / 10 12 / 11 12 / 12 12 / 13 12 / 0 undefined = = = = 1 1 1 0
Integer Remainder Division Examples 12 12 % % % % 1 2 3 4 5 6 7 = = = = 0 0 2 0 5 12 % 8 12 % 9 12 % 10 12 % 11 12 % 12 12 % 13 12 % 0 undefined = = = = 4 3 2 1 0 12
What do the green numbers have in common? 12 12 % % % % 1 2 3 4 5 6 7 = = = = 0 0 2 0 5 12 % 8 12 % 9 12 % 10 12 % 11 12 % 12 12 % 13 12 % 0 undefined = = = = 4 3 2 1 0 12
Flashback To Elementary School Long Division Using / gives you the integer quotient. 4 3)12 12 2 5)13 10 0 15)12 0 0 12)0 0 ? ? ? 0)12 ? ? ? 0 3 12 0 ? ? ? Using % gives you the integer remainder.
// Numeric 02. java 10. 0 + 3. 3333 = 13. 3333 // This program 10. 0 demonstrates the double data type which is used for real numbers – 3. 3333 = 6. 66666667 // It also demonstrates the four real number operations. 10. 0 * 3. 3333 = 33. 3333333 10. 0 / 3. 3333 = 3. 000030000002 public class Numeric 02 { public static void main (String[] args) { double d 1 = 0; double d 2 = 10. 0; double d 3 = 3. 3333; d 1 = d 2 + d 3; // Addition System. out. println(d 2 + " + d 3 + " = " + d 1); d 1 = d 2 – d 3; // Subtraction System. out. println(d 2 + " - " + d 3 + " = " + d 1); d 1 = d 2 * d 3; // Multiplication System. out. println(d 2 + " * " + d 3 + " = " + d 1); d 1 = d 2 / d 3; // Real Number Quotient Division System. out. println(d 2 + " / " + d 3 + " = " + d 1); } } NOTE: Calculations performed with double variables are accurate to 15 decimal places.
Java Real Number Data Types/Operations float 4 bytes double 8 bytes Addition: 6. 75 + 2. 5 = 9. 25 Subtraction: 6. 75 – 2. 5 = 4. 25 Multiplication: 6. 75 * 2. 5 = 16. 875 Real # Quotient Division: 6. 75 / 2. 5 = 2. 7
What About Real # Remainder Division? Java textbooks usually state that remainder or modulus division does not exist for real numbers. Real numbers do not have remainder division in any practical sense. There also is the issue that Java is based on C++, which does not allow remainder division with real number data types. Even though the following examples do not work in C++, they actually do work in Java. 10. 0 % 5. 0 10. 0 % 3. 0 % 10. 0 6. 75 % 2. 5 = = 0. 0 1. 0 3. 0 1. 75
// Numeric 03. java int. Num: 1000 // This program demonstrates memory overflow problems. int. Num: 1000000 // Saving memory is important, but too little memory can // also cause problems. int. Num: 100000 public class Numeric 03 int. Num: -727379968 { public static void main (String[] args) { int. Num = 1000; System. out. println("int. Num: " + int. Num); int. Num = int. Num * 1000; System. out. println("int. Num: " + int. Num); } }
The Odometer Analogy in Decimal 9 9 9 + 0. 1 mile = 0 0 0 When many cars reach 100, 000 miles their odometers cease to be accurate.
The Odometer Analogy with a short integer The largest possible short integer value is 32, 767 which in binary looks like this: 0 1 1 1 1 If we add 1 we get this result: 1 0 0 0 +01 0= 0 0 0 0 32767 + 1 = 32768
How Positive Numbers Give Negative Results The first bit in a number is the sign bit It determines if a number is positive or negative 0 = Positive 1 = Negative 0 1 1 1 1 32767 + 1 = -32768 1 0 0 0 0
Memory Overflow Problems Memory overflow is a situation where the assigned value of a variable exceeds the allocated storage space. The resulting value that is stored will be inaccurate and can change from positive to negative or negative to positive. Avoid memory overflow problems by using a data type that can handle the size of the assigned values. It is important to save computer memory. However, do not be so stingy with memory that overflow problems occur.
// Numeric 04. java num 1: 1. 012345 // This program shows that there is a memory storage limitation to num 2: 1. 0123456789 // how many digits are stored beyond the decimal point. num 3: 1. 0123456789012346 public class Numeric 04 { public static void main (String[] args) { double num 1 = 1. 012345; double num 2 = 1. 0123456789; double num 3 = 1. 0123456789; System. out. println("num 1: " + num 1); System. out. println("num 2: " + num 2); System. out. println("num 3: " + num 3); System. out. println("nn"); } }
// Numeric 05. java num 1: 10. 0 // This program demonstrates another error. num 2: 3. 0 // The program output displays a number that is mathematically incorrect. num 3: 3. 3333335 public class Numeric 05 { public static void main (String[] args) { double num 1 = 10. 0; double num 2 = 3. 0; double num 3 = num 1 / num 2; System. out. println("num 1: " + num 1); System. out. println("num 2: " + num 2); System. out. println("num 3: " + num 3); System. out. println("nn"); } }
// Numeric 06. java num equals 10 // This program shows "unary" arithmetic shortcut notation in Java. numalways equals 11 // Note that "postfix" x++ & "prefix" ++x don't have the same result. num equals public class Numeric 06 { num equals public static void main (String[] args) num equals { int num = 10; System. out. println("num equals " + num); num++; System. out. println("num equals " + num); ++num; System. out. println("num equals " + num); System. out. println("num equals " + num++); System. out. println("num equals " + num); System. out. println("num equals " + ++num); System. out. println("num equals " + num); System. out. println(); } } 12 13 14
Java Unary Operators k++; is the same as: k = k + 1; ++k; is the same as: k = k + 1; k--; is the same as: k = k - 1; --k; is the same as: k = k - 1;
Proper Usage of Shortcuts Proper Usage: k++; System. out. println(k); --k; System. out. println(k); Problematic Usage: System. out. println(k++); System. out. println(--k);
// Numeric 07. java // This program shows arithmetic assignment operations in Java. // x+=10; is the same as x = x + 10; public class Numeric 07 { public static void main (String[] args) { int x = 10; System. out. println("x equals " + x); x += 10; System. out. println("x equals " + x); x -= 10; System. out. println("x equals " + x); x *= 10; System. out. println("x equals " + x); x /= 10; System. out. println("x equals " + x); x %= 10; System. out. println("x equals " + x); System. out. println(); } } x x x equals equals 10 20 10 10 0
Binary Operator Shortcuts No Shortcut Notation k = k + 5; k += 5; k = k - 5; k -= 5; k = k * 5; k *= 5; k = k / 5; k /= 5; k = k % 5; k %= 5;
// Numeric 08. java // This program demonstrates very bad programming style by Before: 10 // combining various shortcuts in one statement. It is difficult // to determine what actually is happening. After: 32 public class Numeric 08 { public static void main (String[] args) { int x = 10; System. out. println("Before: " + x); Do not waste any time x += ++x + x++; trying to figure out System. out. println("After: " + x); the answer is 32. why System. out. println(); The point being made } here is this code is } very confusing and should be avoided.
Shortcut Warning Do not combine shortcuts in one program statement!!! ++num += ++num + num++;
1 // Numeric 09. java 2 // This program demonstrates LValues and RValues. 3 4 public class Numeric 09 5{ 6 public static void main (String[] args) 7 { 8 int n 1 = 10; 9 int n 2 = 100; n 1: 1100 10 int n 3 = 1000; 11 n 1 = n 2 + n 3; n 2: 600 12 System. out. println("n 1: " + n 1); 13 n 2 = n 2 +500; 14 System. out. println("n 2: " + n 2); 15 } 16
LValues & RValues
- Operator pembanding digunakan untuk melakukan? *
- Hát kết hợp bộ gõ cơ thể
- Slidetodoc
- Bổ thể
- Tỉ lệ cơ thể trẻ em
- Gấu đi như thế nào
- Thang điểm glasgow
- Chúa sống lại
- Các môn thể thao bắt đầu bằng tiếng nhảy
- Thế nào là hệ số cao nhất
- Các châu lục và đại dương trên thế giới
- Công thức tính thế năng
- Trời xanh đây là của chúng ta thể thơ
- Cách giải mật thư tọa độ
- Phép trừ bù
- Phản ứng thế ankan
- Các châu lục và đại dương trên thế giới
- Thể thơ truyền thống
- Quá trình desamine hóa có thể tạo ra
- Một số thể thơ truyền thống
- Cái miệng xinh xinh thế chỉ nói điều hay thôi
- Vẽ hình chiếu vuông góc của vật thể sau
- Nguyên nhân của sự mỏi cơ sinh 8
- đặc điểm cơ thể của người tối cổ
- Thứ tự các dấu thăng giáng ở hóa biểu
- Vẽ hình chiếu đứng bằng cạnh của vật thể
- Tia chieu sa te
- Thẻ vin
- đại từ thay thế
- điện thế nghỉ
- Tư thế ngồi viết
- Diễn thế sinh thái là
- Dạng đột biến một nhiễm là
- Bảng số nguyên tố lớn hơn 1000
- Tư thế ngồi viết
- Lời thề hippocrates
- Thiếu nhi thế giới liên hoan
- ưu thế lai là gì