knowt logo

1. Primitive Types

/* 
Writer: SP
Date: March 20th
References: knowt, Fiveable, Barron's, and Collegeboard
Unit 1: Primitive Types */

The Basics

  • Commenting, or using comments is your best friend. It’s a very effective tool, and it’s a good idea to get into the habit of commenting.

  • It will not affect to our program.

  • Types of commenting:

    • //

      • Ex:

//this is a short comment
  • /*…*/

    • Ex:

 /* this is a
long comment*/

Identifiers:

  • An identifier is a name for a variable, parameter, constant, etc.

  • Camelcase

    • An identifier should begin with a lowercase letter, and if there are several words used to name the identifier, then the words after the first should be capitalized.

      • Ex: numOfSides, or testScores

Compiling / Errors / Exceptions:

  • An interpreter is used to change this code into binary(zeros and ones) which is what the computer understands.

  • This process is called compiling.

  • We use IDEs to write out code, or interactive development environments.

  • The code will be checked for programming errors.

  • If any sort of errors are found, then the code stops compiling, and an error message appears.

  • This would be a compile-time error / Syntax Error.  The computer is unable to compile or understand what the code is trying to do, which is why it’s unable to execute properly.

  • Logical error- This is based on the logic of the code. The way that the code is structured, is incorrect.

  • Run-time error- A compiler doesn’t catch this error. It just causes an error with the execution of the program.

  • Exception- It occurs when the program runs and results abnormally.

  • There are 6 exceptions we will learn in AP CSA:

    Exceptions

    When

    ArithmeticException

    when we try to divide an integer by 0.

    ArrayIndexOutOfBoundsException

    When we try to use negative index in array.

    NullPointerException

    We will learn

    IndexOurOfBoundsException

    ‘‘

    StringIndexOutOfBoundsException

    ‘‘

    ConcurrentModificationExceptrion

    ‘‘

Outputs and Inputs:

  • There are 2 ways to produce an output on the screen:

    • Using System.out.print;

    • Using System.out.println;

  • What’s the difference?

    • print() just print.

    • println() print and press ‘enter’

  • Escape Sequence

    Escape Sequence

    Definition

    \n

    new line

    \”

    Double

    \\

    one backslash

// Example

System.out.println("Hello \nthis is \"SP\". \nI stored my file in d:\\myFiles\\. "

/* Output:

Hello
this is "SP".
I stored my file in d:\myFiles\. */

Variables and Assignment:

  • To create an identifier we need to assign a value to the identifier.

  • Note: type identifier = data;

// Example
int age = 30
  • Whenever data is associated with an identifier, it’s referred to as a variable.

    • Ex:

int myFavoriteNumber = 22;
myFavoriteNumber = 78;
System.out.print(“My favorite number is “ + myFavoriteNumber);

Output: My favorite number is 78

We can use ‘final’ keyword to make value cannot change.

The 4 Data Types:

  • Primitive Data- The basic type of data.

  • int- It represents any number that is an integer. So all positive and negative numbers and zero. Integers can’t store decimals!!

  • double - A number that can be positive, negative, or zero, and it can be a fraction or decimal.

  • boolean-A value that is either true or false. True = 1, False = 0 in machine code.

  • char-It represents a single character that can be represented on a computer. It includes any character that you type on the keyboard. All the way from letters, numbers, spaces, and special characters.char values are stored with the use of single quotation marks or an apostrophe.

    • String is a sequence of primitive type char!

Arithmetic Operators:

  • Note: The different operators that are used in Java are the +. - , ,/, and %.

  • The plus sign is used for addition.

    • Ex:

sumOfSides = side2 + side3;
  • The subtraction sign is used for subtraction.

    • Ex:

differenceOfSides = side1 - side3;
  • The asterisk or the star is used for multiplication.

    • Ex:

side1 * side2 = productSide;
  • The slash, or (/) is used for division.

    • Ex:

side3/side2 = quotientSide;
  • The percent sign, (%), is called a modulus operator. It does the division of the numbers, and it returns the remainder.

    • Ex:

sumOfSides % differenceOfSides;
  • Shorten Version (+=, -=, /=, *=, %=)

x+=4;
//x= x + 4

y-=4;
//y= y - 4

z*=4;
//z = z*4

a/=4;
//a = a / 4

b%=4;
//b = b % 4
  • Increment & Decrement (++, —)

x++;
// x = x + 1

y--;
// y = y - 1
  • Just like in normal math, Java also follows an order in which it conducts its operations.

  • Java performs multiplication and division operators, as well as modulus from left to right, then it does addition and subtraction in the same order.

  • If you want to change the order that the operation occurs, just use parentheses to separate the expression out.

Java’s Number System / Casting

  • Java is a crazy language.

int x = 3/2		
  • In the real world, the output of x would be 1.5.

  • However, in Java, we cut off the decimal if it is int.

    • It means the value of x is 1.

  • Casting: Cast a different primitive type. (int <=> double)

int x = 3;
x = (double) x;

// now x is 3.0.

3.0/4 => 0.75
3/4 => 0
(int) 3.0/4 => 0
(double) 3/4 => 0.75
(double) (3/4) => 0.0
  • Different Casting Way:

int x = 5;
double xx = x;

// xx's value is 5.0.

// Vice versa doesn't work.
  • Rounding in Java:

// If we want to round properly in java. Follow this step: 

// Rounding positive number
int roundPositive = (int) (numPositive + 0.5);

// Rounding negative number
int roundNegative = (int) (numNegative - 0.5);
SS

1. Primitive Types

/* 
Writer: SP
Date: March 20th
References: knowt, Fiveable, Barron's, and Collegeboard
Unit 1: Primitive Types */

The Basics

  • Commenting, or using comments is your best friend. It’s a very effective tool, and it’s a good idea to get into the habit of commenting.

  • It will not affect to our program.

  • Types of commenting:

    • //

      • Ex:

//this is a short comment
  • /*…*/

    • Ex:

 /* this is a
long comment*/

Identifiers:

  • An identifier is a name for a variable, parameter, constant, etc.

  • Camelcase

    • An identifier should begin with a lowercase letter, and if there are several words used to name the identifier, then the words after the first should be capitalized.

      • Ex: numOfSides, or testScores

Compiling / Errors / Exceptions:

  • An interpreter is used to change this code into binary(zeros and ones) which is what the computer understands.

  • This process is called compiling.

  • We use IDEs to write out code, or interactive development environments.

  • The code will be checked for programming errors.

  • If any sort of errors are found, then the code stops compiling, and an error message appears.

  • This would be a compile-time error / Syntax Error.  The computer is unable to compile or understand what the code is trying to do, which is why it’s unable to execute properly.

  • Logical error- This is based on the logic of the code. The way that the code is structured, is incorrect.

  • Run-time error- A compiler doesn’t catch this error. It just causes an error with the execution of the program.

  • Exception- It occurs when the program runs and results abnormally.

  • There are 6 exceptions we will learn in AP CSA:

    Exceptions

    When

    ArithmeticException

    when we try to divide an integer by 0.

    ArrayIndexOutOfBoundsException

    When we try to use negative index in array.

    NullPointerException

    We will learn

    IndexOurOfBoundsException

    ‘‘

    StringIndexOutOfBoundsException

    ‘‘

    ConcurrentModificationExceptrion

    ‘‘

Outputs and Inputs:

  • There are 2 ways to produce an output on the screen:

    • Using System.out.print;

    • Using System.out.println;

  • What’s the difference?

    • print() just print.

    • println() print and press ‘enter’

  • Escape Sequence

    Escape Sequence

    Definition

    \n

    new line

    \”

    Double

    \\

    one backslash

// Example

System.out.println("Hello \nthis is \"SP\". \nI stored my file in d:\\myFiles\\. "

/* Output:

Hello
this is "SP".
I stored my file in d:\myFiles\. */

Variables and Assignment:

  • To create an identifier we need to assign a value to the identifier.

  • Note: type identifier = data;

// Example
int age = 30
  • Whenever data is associated with an identifier, it’s referred to as a variable.

    • Ex:

int myFavoriteNumber = 22;
myFavoriteNumber = 78;
System.out.print(“My favorite number is “ + myFavoriteNumber);

Output: My favorite number is 78

We can use ‘final’ keyword to make value cannot change.

The 4 Data Types:

  • Primitive Data- The basic type of data.

  • int- It represents any number that is an integer. So all positive and negative numbers and zero. Integers can’t store decimals!!

  • double - A number that can be positive, negative, or zero, and it can be a fraction or decimal.

  • boolean-A value that is either true or false. True = 1, False = 0 in machine code.

  • char-It represents a single character that can be represented on a computer. It includes any character that you type on the keyboard. All the way from letters, numbers, spaces, and special characters.char values are stored with the use of single quotation marks or an apostrophe.

    • String is a sequence of primitive type char!

Arithmetic Operators:

  • Note: The different operators that are used in Java are the +. - , ,/, and %.

  • The plus sign is used for addition.

    • Ex:

sumOfSides = side2 + side3;
  • The subtraction sign is used for subtraction.

    • Ex:

differenceOfSides = side1 - side3;
  • The asterisk or the star is used for multiplication.

    • Ex:

side1 * side2 = productSide;
  • The slash, or (/) is used for division.

    • Ex:

side3/side2 = quotientSide;
  • The percent sign, (%), is called a modulus operator. It does the division of the numbers, and it returns the remainder.

    • Ex:

sumOfSides % differenceOfSides;
  • Shorten Version (+=, -=, /=, *=, %=)

x+=4;
//x= x + 4

y-=4;
//y= y - 4

z*=4;
//z = z*4

a/=4;
//a = a / 4

b%=4;
//b = b % 4
  • Increment & Decrement (++, —)

x++;
// x = x + 1

y--;
// y = y - 1
  • Just like in normal math, Java also follows an order in which it conducts its operations.

  • Java performs multiplication and division operators, as well as modulus from left to right, then it does addition and subtraction in the same order.

  • If you want to change the order that the operation occurs, just use parentheses to separate the expression out.

Java’s Number System / Casting

  • Java is a crazy language.

int x = 3/2		
  • In the real world, the output of x would be 1.5.

  • However, in Java, we cut off the decimal if it is int.

    • It means the value of x is 1.

  • Casting: Cast a different primitive type. (int <=> double)

int x = 3;
x = (double) x;

// now x is 3.0.

3.0/4 => 0.75
3/4 => 0
(int) 3.0/4 => 0
(double) 3/4 => 0.75
(double) (3/4) => 0.0
  • Different Casting Way:

int x = 5;
double xx = x;

// xx's value is 5.0.

// Vice versa doesn't work.
  • Rounding in Java:

// If we want to round properly in java. Follow this step: 

// Rounding positive number
int roundPositive = (int) (numPositive + 0.5);

// Rounding negative number
int roundNegative = (int) (numNegative - 0.5);
robot