CS 200 - Programming I: Primitives and Expressions
Transcript of CS 200 - Programming I: Primitives and Expressions
![Page 1: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/1.jpg)
CS 200 - Programming I: Primitives andExpressions
Marc Renault
Department of Computer SciencesUniversity of Wisconsin – Madison
Fall 2017TopHat Sec 3 (PM) Join Code: 719946TopHat Sec 4 (AM) Join Code: 891624
![Page 2: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/2.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Java Initiation
![Page 3: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/3.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Basic Output
String LiteralsDouble quotes ("):"I am a string literal"
Concatenation (+):int a = 10; System.out.println("A " + a);
Newline escape character(\n):"First line\nSecond line"
Printing to the ConsolePrint a string:System.out.print("Does not append a new line");
Print a string with a newline:System.out.println("Appends a new line");
1/29
![Page 4: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/4.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Basic Output
String LiteralsDouble quotes ("):"I am a string literal"
Concatenation (+):int a = 10; System.out.println("A " + a);
Newline escape character(\n):"First line\nSecond line"
Printing to the ConsolePrint a string:System.out.print("Does not append a new line");
Print a string with a newline:System.out.println("Appends a new line");
1/29
![Page 5: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/5.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Basic Output
String LiteralsDouble quotes ("):"I am a string literal"
Concatenation (+):int a = 10; System.out.println("A " + a);
Newline escape character(\n):"First line\nSecond line"
Printing to the ConsolePrint a string:System.out.print("Does not append a new line");
Print a string with a newline:System.out.println("Appends a new line");
1/29
![Page 6: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/6.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Basic Output
String LiteralsDouble quotes ("):"I am a string literal"
Concatenation (+):int a = 10; System.out.println("A " + a);
Newline escape character(\n):"First line\nSecond line"
Printing to the ConsolePrint a string:System.out.print("Does not append a new line");
Print a string with a newline:System.out.println("Appends a new line");
1/29
![Page 7: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/7.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
TopHat Question 1
What is the output of:System.out.println("5 and 5 = " + 5 + 5);Type the output.
2/29
![Page 8: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/8.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Basic Input
Using the ScannerInclude the library at the top of the file:import java.util.Scanner;
Create an instance of a Scanner object:Scanner sc = new Scanner(System.in);
Reading an integer:int anInt = sc.nextInt();
3/29
![Page 9: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/9.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Comments and Whitespace
CommentsIgnored by the compiler.Written by the programmer to explain the code.Single-line (//)// Single line comment
Multi-line (/* */)/*** Multi-line comment*/
WhitespaceMostly ignored by the compiler.Good use of white space makes code easier read!
4/29
![Page 10: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/10.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Comments and Whitespace
CommentsIgnored by the compiler.Written by the programmer to explain the code.Single-line (//)// Single line comment
Multi-line (/* */)/*** Multi-line comment*/
WhitespaceMostly ignored by the compiler.Good use of white space makes code easier read!
4/29
![Page 11: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/11.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Compiler Errors and WarningsCompilation Errors
Prevents compilationSyntax errors
Examples:missing ;typo (variable name, keyword)missing braces
Warnings
Warnings don’t stop the compilation process.Good practice to write programs that compile withoutwarnings.For even stricter compilation, use -Xlint:javac -Xlint Foo.java
5/29
![Page 12: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/12.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Compiler Errors and WarningsCompilation Errors
Prevents compilationSyntax errorsExamples:
missing ;typo (variable name, keyword)missing braces
Warnings
Warnings don’t stop the compilation process.Good practice to write programs that compile withoutwarnings.For even stricter compilation, use -Xlint:javac -Xlint Foo.java
5/29
![Page 13: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/13.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Compiler Errors and WarningsCompilation Errors
Prevents compilationSyntax errorsExamples:
missing ;typo (variable name, keyword)missing braces
WarningsWarnings don’t stop the compilation process.
Good practice to write programs that compile withoutwarnings.For even stricter compilation, use -Xlint:javac -Xlint Foo.java
5/29
![Page 14: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/14.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Compiler Errors and WarningsCompilation Errors
Prevents compilationSyntax errorsExamples:
missing ;typo (variable name, keyword)missing braces
WarningsWarnings don’t stop the compilation process.Good practice to write programs that compile withoutwarnings.
For even stricter compilation, use -Xlint:javac -Xlint Foo.java
5/29
![Page 15: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/15.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Compiler Errors and WarningsCompilation Errors
Prevents compilationSyntax errorsExamples:
missing ;typo (variable name, keyword)missing braces
WarningsWarnings don’t stop the compilation process.Good practice to write programs that compile withoutwarnings.For even stricter compilation, use -Xlint:javac -Xlint Foo.java
5/29
![Page 16: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/16.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Primitives
![Page 17: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/17.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Two-state machineBinary
Data UnitsBit The smallest unit of information. 0 or 1, false or
true, off or on, low or high, ...
Nibble 4-bits.Byte 8-bits.Word A group of bytes, depends on the systems: 16-bit
systems is 2 byte words, 32-bit is is 4 byte words, ...KB vs kB 1024 bytes vs 1000 bytes.
MiB vs MB 10242 bytes vs 10002 bytes.GiB vs GB 10243 bytes vs 10003 bytes.
6/29
![Page 18: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/18.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Two-state machineBinary
Data UnitsBit The smallest unit of information. 0 or 1, false or
true, off or on, low or high, ...
Nibble 4-bits.
Byte 8-bits.
Word A group of bytes, depends on the systems: 16-bitsystems is 2 byte words, 32-bit is is 4 byte words, ...
KB vs kB 1024 bytes vs 1000 bytes.MiB vs MB 10242 bytes vs 10002 bytes.GiB vs GB 10243 bytes vs 10003 bytes.
6/29
![Page 19: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/19.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Two-state machineBinary
Data UnitsBit The smallest unit of information. 0 or 1, false or
true, off or on, low or high, ...Nibble 4-bits.
Byte 8-bits.
Word A group of bytes, depends on the systems: 16-bitsystems is 2 byte words, 32-bit is is 4 byte words, ...
KB vs kB 1024 bytes vs 1000 bytes.MiB vs MB 10242 bytes vs 10002 bytes.GiB vs GB 10243 bytes vs 10003 bytes.
6/29
![Page 20: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/20.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Two-state machineBinary
Data UnitsBit The smallest unit of information. 0 or 1, false or
true, off or on, low or high, ...Nibble 4-bits.
Byte 8-bits.Word A group of bytes, depends on the systems: 16-bit
systems is 2 byte words, 32-bit is is 4 byte words, ...
KB vs kB 1024 bytes vs 1000 bytes.MiB vs MB 10242 bytes vs 10002 bytes.GiB vs GB 10243 bytes vs 10003 bytes.
6/29
![Page 21: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/21.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Two-state machineBinary
Data UnitsBit The smallest unit of information. 0 or 1, false or
true, off or on, low or high, ...Nibble 4-bits.
Byte 8-bits.Word A group of bytes, depends on the systems: 16-bit
systems is 2 byte words, 32-bit is is 4 byte words, ...KB vs kB 1024 bytes vs 1000 bytes.
MiB vs MB 10242 bytes vs 10002 bytes.GiB vs GB 10243 bytes vs 10003 bytes.
6/29
![Page 22: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/22.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Two-state machineBinary
Data UnitsBit The smallest unit of information. 0 or 1, false or
true, off or on, low or high, ...Nibble 4-bits.
Byte 8-bits.Word A group of bytes, depends on the systems: 16-bit
systems is 2 byte words, 32-bit is is 4 byte words, ...KB vs kB 1024 bytes vs 1000 bytes.
MiB vs MB 10242 bytes vs 10002 bytes.
GiB vs GB 10243 bytes vs 10003 bytes.
6/29
![Page 23: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/23.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Two-state machineBinary
Data UnitsBit The smallest unit of information. 0 or 1, false or
true, off or on, low or high, ...Nibble 4-bits.
Byte 8-bits.Word A group of bytes, depends on the systems: 16-bit
systems is 2 byte words, 32-bit is is 4 byte words, ...KB vs kB 1024 bytes vs 1000 bytes.
MiB vs MB 10242 bytes vs 10002 bytes.GiB vs GB 10243 bytes vs 10003 bytes.
6/29
![Page 24: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/24.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Variable Declaration
type variableName = expression;
TypeJava primitives:
int, byte, char, short, long, float and double.
Restricts the variable to:
A set of values.A set of operations.
Java is a strongly typed language.All variables are declared with a specific type and remainthat type.
7/29
![Page 25: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/25.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Variable Declaration
type variableName = expression;
TypeJava primitives:
int, byte, char, short, long, float and double.Restricts the variable to:
A set of values.A set of operations.
Java is a strongly typed language.All variables are declared with a specific type and remainthat type.
7/29
![Page 26: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/26.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Variable Declaration
type variableName = expression;
TypeJava primitives:
int, byte, char, short, long, float and double.Restricts the variable to:
A set of values.
A set of operations.Java is a strongly typed language.All variables are declared with a specific type and remainthat type.
7/29
![Page 27: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/27.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Variable Declaration
type variableName = expression;
TypeJava primitives:
int, byte, char, short, long, float and double.Restricts the variable to:
A set of values.A set of operations.
Java is a strongly typed language.All variables are declared with a specific type and remainthat type.
7/29
![Page 28: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/28.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Variable Declaration
type variableName = expression;
TypeJava primitives:
int, byte, char, short, long, float and double.Restricts the variable to:
A set of values.A set of operations.
Java is a strongly typed language.
All variables are declared with a specific type and remainthat type.
7/29
![Page 29: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/29.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Variable Declaration
type variableName = expression;
TypeJava primitives:
int, byte, char, short, long, float and double.Restricts the variable to:
A set of values.A set of operations.
Java is a strongly typed language.All variables are declared with a specific type and remainthat type.
7/29
![Page 30: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/30.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
A rose by any other name...Naming Rules
Starts with a letter, $, or underscore (_).
Subsequent characters can be letters, $, (_) or digits.Cannot be a keyword (Ex: int, case, while)Can be any length.Case-senstive: foobar 6= fooBar
CS 200 Style and Good Practice
Style guide:https://cs200-www.cs.wisc.edu/wp/style-guide/
Use lowerCamelCase: thisIsLowerCamelCaseDon’t use $ or _Length: not too long, but clearly identify the role.
8/29
![Page 31: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/31.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
A rose by any other name...Naming Rules
Starts with a letter, $, or underscore (_).Subsequent characters can be letters, $, (_) or digits.
Cannot be a keyword (Ex: int, case, while)Can be any length.Case-senstive: foobar 6= fooBar
CS 200 Style and Good Practice
Style guide:https://cs200-www.cs.wisc.edu/wp/style-guide/
Use lowerCamelCase: thisIsLowerCamelCaseDon’t use $ or _Length: not too long, but clearly identify the role.
8/29
![Page 32: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/32.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
A rose by any other name...Naming Rules
Starts with a letter, $, or underscore (_).Subsequent characters can be letters, $, (_) or digits.Cannot be a keyword (Ex: int, case, while)
Can be any length.Case-senstive: foobar 6= fooBar
CS 200 Style and Good Practice
Style guide:https://cs200-www.cs.wisc.edu/wp/style-guide/
Use lowerCamelCase: thisIsLowerCamelCaseDon’t use $ or _Length: not too long, but clearly identify the role.
8/29
![Page 33: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/33.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
A rose by any other name...Naming Rules
Starts with a letter, $, or underscore (_).Subsequent characters can be letters, $, (_) or digits.Cannot be a keyword (Ex: int, case, while)Can be any length.
Case-senstive: foobar 6= fooBar
CS 200 Style and Good Practice
Style guide:https://cs200-www.cs.wisc.edu/wp/style-guide/
Use lowerCamelCase: thisIsLowerCamelCaseDon’t use $ or _Length: not too long, but clearly identify the role.
8/29
![Page 34: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/34.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
A rose by any other name...Naming Rules
Starts with a letter, $, or underscore (_).Subsequent characters can be letters, $, (_) or digits.Cannot be a keyword (Ex: int, case, while)Can be any length.Case-senstive: foobar 6= fooBar
CS 200 Style and Good Practice
Style guide:https://cs200-www.cs.wisc.edu/wp/style-guide/
Use lowerCamelCase: thisIsLowerCamelCaseDon’t use $ or _Length: not too long, but clearly identify the role.
8/29
![Page 35: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/35.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
A rose by any other name...Naming Rules
Starts with a letter, $, or underscore (_).Subsequent characters can be letters, $, (_) or digits.Cannot be a keyword (Ex: int, case, while)Can be any length.Case-senstive: foobar 6= fooBar
CS 200 Style and Good Practice
Style guide:https://cs200-www.cs.wisc.edu/wp/style-guide/
Use lowerCamelCase: thisIsLowerCamelCaseDon’t use $ or _Length: not too long, but clearly identify the role.
8/29
![Page 36: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/36.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
A rose by any other name...Naming Rules
Starts with a letter, $, or underscore (_).Subsequent characters can be letters, $, (_) or digits.Cannot be a keyword (Ex: int, case, while)Can be any length.Case-senstive: foobar 6= fooBar
CS 200 Style and Good PracticeStyle guide:https://cs200-www.cs.wisc.edu/wp/style-guide/
Use lowerCamelCase: thisIsLowerCamelCaseDon’t use $ or _Length: not too long, but clearly identify the role.
8/29
![Page 37: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/37.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
A rose by any other name...Naming Rules
Starts with a letter, $, or underscore (_).Subsequent characters can be letters, $, (_) or digits.Cannot be a keyword (Ex: int, case, while)Can be any length.Case-senstive: foobar 6= fooBar
CS 200 Style and Good PracticeStyle guide:https://cs200-www.cs.wisc.edu/wp/style-guide/
Use lowerCamelCase: thisIsLowerCamelCase
Don’t use $ or _Length: not too long, but clearly identify the role.
8/29
![Page 38: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/38.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
A rose by any other name...Naming Rules
Starts with a letter, $, or underscore (_).Subsequent characters can be letters, $, (_) or digits.Cannot be a keyword (Ex: int, case, while)Can be any length.Case-senstive: foobar 6= fooBar
CS 200 Style and Good PracticeStyle guide:https://cs200-www.cs.wisc.edu/wp/style-guide/
Use lowerCamelCase: thisIsLowerCamelCaseDon’t use $ or _
Length: not too long, but clearly identify the role.
8/29
![Page 39: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/39.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
A rose by any other name...Naming Rules
Starts with a letter, $, or underscore (_).Subsequent characters can be letters, $, (_) or digits.Cannot be a keyword (Ex: int, case, while)Can be any length.Case-senstive: foobar 6= fooBar
CS 200 Style and Good PracticeStyle guide:https://cs200-www.cs.wisc.edu/wp/style-guide/
Use lowerCamelCase: thisIsLowerCamelCaseDon’t use $ or _Length: not too long, but clearly identify the role.
8/29
![Page 40: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/40.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Type: int
Declaration:int myInt = 2;
ValuesInteger in 4 bytes (32 bits)
−2, 147, 483, 648 to 2, 147, 483, 647 inclusive
Other integer typesbyteshortlong
1 byte2 bytes8 bytes
−128 to 127−32, 768 to 32, 767−9, 223, 372, 036, 854, 775, 808 to 9, ..., 807
9/29
![Page 41: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/41.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Type: int
Declaration:int myInt = 2;
ValuesInteger in 4 bytes (32 bits)−2, 147, 483, 648 to 2, 147, 483, 647 inclusive
Other integer typesbyteshortlong
1 byte2 bytes8 bytes
−128 to 127−32, 768 to 32, 767−9, 223, 372, 036, 854, 775, 808 to 9, ..., 807
9/29
![Page 42: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/42.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Type: int
Declaration:int myInt = 2;
ValuesInteger in 4 bytes (32 bits)−2, 147, 483, 648 to 2, 147, 483, 647 inclusive
Other integer typesbyteshortlong
1 byte2 bytes8 bytes
−128 to 127−32, 768 to 32, 767−9, 223, 372, 036, 854, 775, 808 to 9, ..., 807
9/29
![Page 43: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/43.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Type: double
Declaration:double myDbl = 1.618;
ValuesReal (floating point) number in 8 bytes (64 bits)
About 15 digits of precisionRange is approx. ±1.7 × 10308
Another real type: float
Real (floating point) number in 4 bytes (32 bits)About 6 digits of precisionRange is approx. ±3.4× 1038
10/29
![Page 44: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/44.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Type: double
Declaration:double myDbl = 1.618;
ValuesReal (floating point) number in 8 bytes (64 bits)About 15 digits of precision
Range is approx. ±1.7 × 10308
Another real type: float
Real (floating point) number in 4 bytes (32 bits)About 6 digits of precisionRange is approx. ±3.4× 1038
10/29
![Page 45: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/45.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Type: double
Declaration:double myDbl = 1.618;
ValuesReal (floating point) number in 8 bytes (64 bits)About 15 digits of precisionRange is approx. ±1.7 × 10308
Another real type: float
Real (floating point) number in 4 bytes (32 bits)About 6 digits of precisionRange is approx. ±3.4× 1038
10/29
![Page 46: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/46.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Type: double
Declaration:double myDbl = 1.618;
ValuesReal (floating point) number in 8 bytes (64 bits)About 15 digits of precisionRange is approx. ±1.7 × 10308
Another real type: float
Real (floating point) number in 4 bytes (32 bits)About 6 digits of precisionRange is approx. ±3.4× 1038
10/29
![Page 47: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/47.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Type: double
Declaration:double myDbl = 1.618;
ValuesReal (floating point) number in 8 bytes (64 bits)About 15 digits of precisionRange is approx. ±1.7 × 10308
Another real type: floatReal (floating point) number in 4 bytes (32 bits)
About 6 digits of precisionRange is approx. ±3.4× 1038
10/29
![Page 48: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/48.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Type: double
Declaration:double myDbl = 1.618;
ValuesReal (floating point) number in 8 bytes (64 bits)About 15 digits of precisionRange is approx. ±1.7 × 10308
Another real type: floatReal (floating point) number in 4 bytes (32 bits)About 6 digits of precision
Range is approx. ±3.4× 1038
10/29
![Page 49: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/49.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Type: double
Declaration:double myDbl = 1.618;
ValuesReal (floating point) number in 8 bytes (64 bits)About 15 digits of precisionRange is approx. ±1.7 × 10308
Another real type: floatReal (floating point) number in 4 bytes (32 bits)About 6 digits of precisionRange is approx. ±3.4× 1038
10/29
![Page 50: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/50.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
TopHat Question 2
What is not a double literal?a. 0.5b. .5c. 5d. 5.5e5
11/29
![Page 51: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/51.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Constant Variable
final type VARIABLE_NAME = expression;
finalModifierCannot change the value after initialization.
CS 200 Style and Good PracticeUse constants to keep any hard-coded data in one place.Names are in ALL CAPS with _ between words. E.g.:SOME_CONST_NAME
12/29
![Page 52: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/52.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Constant Variable
final type VARIABLE_NAME = expression;
finalModifierCannot change the value after initialization.
CS 200 Style and Good PracticeUse constants to keep any hard-coded data in one place.Names are in ALL CAPS with _ between words. E.g.:SOME_CONST_NAME
12/29
![Page 53: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/53.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Expressions
![Page 54: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/54.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Assignments
someVar = expression
Assigment Operator: =Right-to-left associative:
1 Evaluate expression.2 Assign the value of expression to someVar
E.g:a = b = c = d = 5 + 5;
TopHat Question 3: What is the value of a?
13/29
![Page 55: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/55.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Assignments
someVar = expression
Assigment Operator: =Right-to-left associative:
1 Evaluate expression.
2 Assign the value of expression to someVar
E.g:a = b = c = d = 5 + 5;
TopHat Question 3: What is the value of a?
13/29
![Page 56: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/56.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Assignments
someVar = expression
Assigment Operator: =Right-to-left associative:
1 Evaluate expression.2 Assign the value of expression to someVar
E.g:a = b = c = d = 5 + 5;
TopHat Question 3: What is the value of a?
13/29
![Page 57: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/57.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Assignments
someVar = expression
Assigment Operator: =Right-to-left associative:
1 Evaluate expression.2 Assign the value of expression to someVar
E.g:a = b = c = d = 5 + 5;
TopHat Question 3: What is the value of a?
13/29
![Page 58: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/58.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Assignments
someVar = expression
Assigment Operator: =Right-to-left associative:
1 Evaluate expression.2 Assign the value of expression to someVar
E.g:a = b = c = d = 5 + 5;TopHat Question 3: What is the value of a?
13/29
![Page 59: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/59.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Swapping Variables
Consider the following code snippet:
int a = 2;int b = 3;
TopHat Question 4: How can we swap the values?
14/29
![Page 60: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/60.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Swapping Variables
Consider the following code snippet:
int a = 2;int b = 3;
TopHat Question 4: How can we swap the values?
14/29
![Page 61: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/61.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Operators
Arithmetic OperatorsUnary:
-: NegationBinary:
+: Addition-: Subtraction*: Multiplication/: Division%: Remainder (modulo)
Compound:
+=: Add and assignE.g. a += b is shorthand for a = a + bOther variants: -=, *=, /=, %=
15/29
![Page 62: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/62.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Operators
Arithmetic OperatorsUnary:
-: Negation
Binary:
+: Addition-: Subtraction*: Multiplication/: Division%: Remainder (modulo)
Compound:
+=: Add and assignE.g. a += b is shorthand for a = a + bOther variants: -=, *=, /=, %=
15/29
![Page 63: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/63.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Operators
Arithmetic OperatorsUnary:
-: NegationBinary:
+: Addition-: Subtraction*: Multiplication/: Division%: Remainder (modulo)
Compound:
+=: Add and assignE.g. a += b is shorthand for a = a + bOther variants: -=, *=, /=, %=
15/29
![Page 64: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/64.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Operators
Arithmetic OperatorsUnary:
-: NegationBinary:
+: Addition
-: Subtraction*: Multiplication/: Division%: Remainder (modulo)
Compound:
+=: Add and assignE.g. a += b is shorthand for a = a + bOther variants: -=, *=, /=, %=
15/29
![Page 65: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/65.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Operators
Arithmetic OperatorsUnary:
-: NegationBinary:
+: Addition-: Subtraction
*: Multiplication/: Division%: Remainder (modulo)
Compound:
+=: Add and assignE.g. a += b is shorthand for a = a + bOther variants: -=, *=, /=, %=
15/29
![Page 66: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/66.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Operators
Arithmetic OperatorsUnary:
-: NegationBinary:
+: Addition-: Subtraction*: Multiplication
/: Division%: Remainder (modulo)
Compound:
+=: Add and assignE.g. a += b is shorthand for a = a + bOther variants: -=, *=, /=, %=
15/29
![Page 67: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/67.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Operators
Arithmetic OperatorsUnary:
-: NegationBinary:
+: Addition-: Subtraction*: Multiplication/: Division
%: Remainder (modulo)Compound:
+=: Add and assignE.g. a += b is shorthand for a = a + bOther variants: -=, *=, /=, %=
15/29
![Page 68: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/68.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Operators
Arithmetic OperatorsUnary:
-: NegationBinary:
+: Addition-: Subtraction*: Multiplication/: Division%: Remainder (modulo)
Compound:
+=: Add and assignE.g. a += b is shorthand for a = a + bOther variants: -=, *=, /=, %=
15/29
![Page 69: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/69.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Operators
Arithmetic OperatorsUnary:
-: NegationBinary:
+: Addition-: Subtraction*: Multiplication/: Division%: Remainder (modulo)
Compound:
+=: Add and assignE.g. a += b is shorthand for a = a + bOther variants: -=, *=, /=, %=
15/29
![Page 70: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/70.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Operators
Arithmetic OperatorsUnary:
-: NegationBinary:
+: Addition-: Subtraction*: Multiplication/: Division%: Remainder (modulo)
Compound:+=: Add and assignE.g. a += b is shorthand for a = a + b
Other variants: -=, *=, /=, %=
15/29
![Page 71: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/71.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Operators
Arithmetic OperatorsUnary:
-: NegationBinary:
+: Addition-: Subtraction*: Multiplication/: Division%: Remainder (modulo)
Compound:+=: Add and assignE.g. a += b is shorthand for a = a + bOther variants: -=, *=, /=, %=
15/29
![Page 72: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/72.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Operator Precedence
CS 200 Style and Good PracticeUse parentheses to explicitly indicate the precedence.
16/29
![Page 73: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/73.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Operator Precedence
CS 200 Style and Good PracticeUse parentheses to explicitly indicate the precedence.
16/29
![Page 74: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/74.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Type Conversion
byte→ short→ int→ long→ float→ doubleNarrowest Widest
Implicit ConversionArithmetic operators: Implicit cast to the widest type.Assignment: Implicit cast RHS to LHS type if wider. E.g.double dbl = 2;
Explicit Conversion: Casting(type)E.g.int i = (int)2.5;
17/29
![Page 75: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/75.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Type Conversion
byte→ short→ int→ long→ float→ doubleNarrowest Widest
Implicit ConversionArithmetic operators: Implicit cast to the widest type.Assignment: Implicit cast RHS to LHS type if wider. E.g.double dbl = 2;
Explicit Conversion: Casting(type)E.g.int i = (int)2.5;
17/29
![Page 76: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/76.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Type Conversion
byte→ short→ int→ long→ float→ doubleNarrowest Widest
Implicit ConversionArithmetic operators: Implicit cast to the widest type.Assignment: Implicit cast RHS to LHS type if wider. E.g.double dbl = 2;
Explicit Conversion: Casting(type)E.g.int i = (int)2.5;
17/29
![Page 77: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/77.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Integer Division
Truncation with /When both operands are integers (int, short, etc), theresult will also be an integer.
Java will truncate the result: Chop off the decimal.E.g.:int a = 3 / 2; //Sets a to 1double b = (double)(3 / 2); // Still 1double c = (double)3 / 2; // Now 1.5
18/29
![Page 78: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/78.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Integer Division
Truncation with /When both operands are integers (int, short, etc), theresult will also be an integer.Java will truncate the result: Chop off the decimal.
E.g.:int a = 3 / 2; //Sets a to 1double b = (double)(3 / 2); // Still 1double c = (double)3 / 2; // Now 1.5
18/29
![Page 79: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/79.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Integer Division
Truncation with /When both operands are integers (int, short, etc), theresult will also be an integer.Java will truncate the result: Chop off the decimal.E.g.:int a = 3 / 2; //Sets a to 1double b = (double)(3 / 2); // Still 1double c = (double)3 / 2; // Now 1.5
18/29
![Page 80: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/80.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Simple Memory ModelMemory
0xffffffff
0xff000000
Key PointsVariables are allocatedslots in memory with amemory address.The value of the variablesare stored in the assignedmemory slots.
19/29
![Page 81: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/81.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Simple Memory ModelMemory
0xffffffff
6 i0xff000000
int i = 6;
Key PointsVariables are allocatedslots in memory with amemory address.The value of the variablesare stored in the assignedmemory slots.
19/29
![Page 82: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/82.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Simple Memory ModelMemory
0xffffffff
6 i6 j
0xff000000
int i = 6;int j = 6;
Key PointsVariables are allocatedslots in memory with amemory address.The value of the variablesare stored in the assignedmemory slots.
19/29
![Page 83: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/83.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Simple Memory ModelMemory
0xffffffff
6 i6 j
2.5 d
0xff000000
int i = 6;int j = 6;double d = 2.5;
Key PointsVariables are allocatedslots in memory with amemory address.The value of the variablesare stored in the assignedmemory slots.
19/29
![Page 84: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/84.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Simple Memory ModelMemory
0xffffffff
6 i6 j
2.5 d25 s
0xff000000
int i = 6;int j = 6;double d = 2.5;short s = 25;
Key PointsVariables are allocatedslots in memory with amemory address.The value of the variablesare stored in the assignedmemory slots.
19/29
![Page 85: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/85.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Simple Memory ModelMemory
0xffffffff
6 i6 j
2.5 d25 s
0xff000000
int i = 6;int j = 6;double d = 2.5;short s = 25;
Key PointsVariables are allocatedslots in memory with amemory address.The value of the variablesare stored in the assignedmemory slots.
19/29
![Page 86: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/86.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Using Libraries
![Page 87: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/87.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Libraries
Code LibrariesA collection of classes and methods.Java comes with a set of standard libraries such asjava.util.Scanner and java.lang.Math
3rd Party Libraries also exist.
20/29
![Page 88: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/88.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Math Class
Standard Java class:import java.lang.Math;
Basic Numeric OperationsCollection of static methods and constants.Methods: pow, abs, ceil, etc...Members: E, PIAPI: https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html
21/29
![Page 89: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/89.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Methods
![Page 90: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/90.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Methods and Functions
What is a Method?Evolved from mathematical functions, e.g. f (x) = 2x + 1
A parameterized block of code the performs a sequence ofinstructions and returns a single value.Many parameters, but only 1 return value.
Why use Methods?Abstraction: modular design.End of redundant code: tempted to copy-and-paste code;make a method.Good coding practice: easier to read.
22/29
![Page 91: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/91.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Methods and Functions
What is a Method?Evolved from mathematical functions, e.g. f (x) = 2x + 1A parameterized block of code the performs a sequence ofinstructions and returns a single value.
Many parameters, but only 1 return value.
Why use Methods?Abstraction: modular design.End of redundant code: tempted to copy-and-paste code;make a method.Good coding practice: easier to read.
22/29
![Page 92: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/92.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Methods and Functions
What is a Method?Evolved from mathematical functions, e.g. f (x) = 2x + 1A parameterized block of code the performs a sequence ofinstructions and returns a single value.Many parameters, but only 1 return value.
Why use Methods?Abstraction: modular design.End of redundant code: tempted to copy-and-paste code;make a method.Good coding practice: easier to read.
22/29
![Page 93: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/93.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Methods and Functions
What is a Method?Evolved from mathematical functions, e.g. f (x) = 2x + 1A parameterized block of code the performs a sequence ofinstructions and returns a single value.Many parameters, but only 1 return value.
Why use Methods?Abstraction: modular design.End of redundant code: tempted to copy-and-paste code;make a method.Good coding practice: easier to read.
22/29
![Page 94: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/94.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Structure
E.g.
modifiers returnType methodName(pType1 pName1, . . .) {body
}
Method TerminologyModifiers E.g. public, static, final.
Header Modifier, return type, method name, and the typesand names of the parameters.
Signature Method name and the types of the parameters.Body Block of code containing the method’s
instructions.Definition The header and the body.
23/29
![Page 95: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/95.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Structure
E.g.public static double avgTwoInts(int p1, int p2) {
return (p1 + p2)/2.0;}
Method TerminologyModifiers E.g. public, static, final.
Header Modifier, return type, method name, and the typesand names of the parameters.
Signature Method name and the types of the parameters.Body Block of code containing the method’s
instructions.Definition The header and the body.
23/29
![Page 96: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/96.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Using Method
public static double avgTwoInts(int p1, int p2) {return (p1 + p2)/2.0;
}
Calling a MethodavgTwoInts(5, 7)
methodName(arg1, arg2, ...)
The call of the method is evaluated to the return value of themethod, using the arguments provided.
24/29
![Page 97: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/97.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Parameters
public static double avgTwoInts(int p1, int p2) {return (p1 + p2)/2.0;
}
Method ParametersScope: considered as variables within the body of thefunction.Passed by value: changing the value in the method doesn’tchange the passed value.Initialized at the moment of the method call.
25/29
![Page 98: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/98.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Return Value
Method Value (return)The method is evaluated with the passed parameters.voidmethods do not return a value.All other methods return a single value.
Method Side EffectsAnything in a method that changes the state of the runningprogram is considered a side effect, such as:
Printing to the screen.Reading input from the user.Changing global variables.
26/29
![Page 99: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/99.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Return Value
Method Value (return)The method is evaluated with the passed parameters.voidmethods do not return a value.All other methods return a single value.
Method Side EffectsAnything in a method that changes the state of the runningprogram is considered a side effect, such as:
Printing to the screen.Reading input from the user.Changing global variables.
26/29
![Page 100: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/100.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Return Value
Method Value (return)The method is evaluated with the passed parameters.voidmethods do not return a value.All other methods return a single value.
Method Side EffectsAnything in a method that changes the state of the runningprogram is considered a side effect, such as:
Printing to the screen.Reading input from the user.Changing global variables.
26/29
![Page 101: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/101.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
TopHat Question 5
What is the value of the following method when called bymyMethod(10):
public static int myMethod(int i) {System.out.println("Value is " + i);return 6;
}
a. 6b. 10c. Value is 10d. void
27/29
![Page 102: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/102.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Method CommentsCS 200 and Good Practice
/*** Calculates the average of two integer values.** @param p1 First integer value.* @param p2 Second integer value.* @return Average of p1 and p2.*/
public static double avgTwoInts(int p1, int p2) {return (p1 + p2 )/2.0;
}
Javadoc Comments1 An explanation of the method.2 @param An explanation for each parameter.3 @return An explanation of the value returned.
28/29
![Page 103: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/103.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Method CommentsCS 200 and Good Practice
/*** Calculates the average of two integer values.** @param p1 First integer value.* @param p2 Second integer value.* @return Average of p1 and p2.*/
public static double avgTwoInts(int p1, int p2) {return (p1 + p2 )/2.0;
}
Javadoc Comments1 An explanation of the method.2 @param An explanation for each parameter.3 @return An explanation of the value returned.
28/29
![Page 104: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/104.jpg)
Java Initiation Primitives Expressions Using Libraries Methods
Further Reading
COMP SCI 200: Programming IzyBooks.com, 2015.zyBook code:WISCCOMPSCI200Fall2017
Chapter 1. Programming ProcessChapter 2. Primitives and Expressions
29/29
![Page 105: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/105.jpg)
Appendix References
Appendix
![Page 106: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/106.jpg)
Appendix References
References
![Page 107: CS 200 - Programming I: Primitives and Expressions](https://reader035.fdocuments.us/reader035/viewer/2022081505/62a3190936d92f1f5a00e475/html5/thumbnails/107.jpg)
Appendix References
Image Sources I
https://brand.wisc.edu/web/logos/
http://www.zybooks.com/
30/29