0% found this document useful (0 votes)
24 views

AP Computer Science Class: Primitive Type

This document provides an overview of key concepts in Java including: 1. Inline comments in Java use // and allow leaving secret messages in code for reviewers. 2. println() displays text and moves to a new line while print() displays without moving to a new line. 3. Every Java statement must end with a semicolon to indicate the end of an instruction to the compiler. 4. Variables are declared with a data type like int or String followed by the variable name.

Uploaded by

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

AP Computer Science Class: Primitive Type

This document provides an overview of key concepts in Java including: 1. Inline comments in Java use // and allow leaving secret messages in code for reviewers. 2. println() displays text and moves to a new line while print() displays without moving to a new line. 3. Every Java statement must end with a semicolon to indicate the end of an instruction to the compiler. 4. Variables are declared with a data type like int or String followed by the variable name.

Uploaded by

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

AP Computer Science class

Primitive Type
1. Inline comment
• 2 slashes: //
• Function: Tell someone a secret message. The compiler will not render inline comment
Eg: you write a long project at a school and you have to explain your professors when they
read your code:
// The first step is …
*Your code here *
// The second step is …
• Your code *
// The third step is

2. Difference
between println()
and print()
• Write down these code and
tell me the difference
between println() and print()
2. Difference
between println()
and print()
• Write down these code and
tell me the difference
between println() and print()
2. Difference
between
println() and
print()
2. Difference between println() and print()

• Println(): To display some text and move the cursor onto the next line
• Print(): To display some text and NOT move the cursor onto the next line
3. Semicolon

• The semicolon is a special character that signifies the end


of an instruction. Every distinct line of code should end
with a semicolon. It tells the compiler that an instruction
ends here.
4. General Form for declaring a variable in Java

Datatype variableName;
Eg:
int number;
double gpa;
String name;
boolean fact;
4. General Form for declaring a variable and initialize it
in Java

Datatype variableName = value;


Eg:
int number = 4;
double gpa = 3.81;
String name = Thu;
boolean fact = true;
5. Naming variable: Camel case

• All primitive variable names start with a lowercase letter;


then for each new word in the variable name, the first
letter of the new word is assigned an uppercase letter.
• For eg:
myName, myMoney, theNumberOfStudents,
theNumberOfCoins,…
• Variable’s name cannot start with number or symbol:
• For eg: these are wrong
• int 5dollars;
• int #tickets;
• A constant is a name for a variable whose value
doesn’t change during the run of a program. Any
attempt to change the value of the constant will
generate a compiler error.
6. Constants • The Java naming convention is to capitalize each
letter of the constant name and to include
underscores (“_”) when two or more words are
combined.
7. The int and double type

• Decimals can be stored only in the double data type


Eg: double number = 3.97 not int number = 3.97
• Integers can be stored either double or int data type.
Eg: you can write double number = 4 or int number = 4
•If you want to store a value that is not a
7. The Boolean number, but rather a true or false value, then
you should choose a boolean variable
type
•8.1. Mod (symbol: %)

8. Mathematical •Example: 14 divide by 3 is 4 with a remainder 2.


•So the mod is 2.
Operation •-> 14 % 3 = 2
8. Mathematical •8.2. Other arithmetic operation
Operation
8. Mathematical Operation

8.3. The int and double division:


Try to write these in Replit and tell me the data type of each
output:
Q1: 10.0 / 4.0 -> double or int?
Q2: 10.0 / 4 -> double or int?
Q3: 10 / 4.0 -> double or int?
Q4: 10 / 4 -> double or int?
8. Mathematical •8.3. The int and double division:
Operation
8. Mathematical Operation

8.4. Arithmetic Exception:


Dividing by zero or performing a mod by zero will produce
an error called an ArithmeticException.
Eg:
9. Modifying Number Variables
9. Modifying Number Variables

Shortcut:
int score = 0;
score += 100;
9. Modifying
Number Variables

•Predict results of these codes:


9. Modifying
Number Variables

•Increment:
•To increment a variable means to
add to the value stored in the
variable
10. Casting a variable

• On the AP Computer Science A Exam, you will be tested on


the correct way to cast variables.
• Casting is a way to tell a variable to temporarily become a
different data type for the sake of performing some action,
such as division.
10. Casting a variable Predict the outputs:
10. Casting a variable
Manually rounding the output: to round the output,
cast from a double to an int
11. Types of errors:

1. Compile errors:
• Forgetting a semicolon at the end of an instruction
• Forgetting to put a data type for a variable
• Using a keyword as a variable name
• Forgetting to initialize a variable
• Forgetting a curly brace (a curly brace doesn’t have a
partner)
11. Types of errors:

2. Runtime exception (later chapters)


If your program crashes while it is running, then you
have a run-time error. The compiler will display the
error and it may have the word exception in it.
3. Logic errors:
You will have to analyze code in the multiple-choice
section of the exam and find hidden logic errors.

You might also like