0% found this document useful (0 votes)
5 views11 pages

Java Primitive Types and Variables Guide

Uploaded by

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

Java Primitive Types and Variables Guide

Uploaded by

ghadiandraous
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Primitive Types:

7.1: Introduction to Java

Java Skeleton: Everything in the .java file must exist within the program’s
class.

The ‘main’ method executes the code within the curly brackets.

‘[Link].___’ = How the message should be output into the console


program.

‘(“”)’ = Anything enclosed in quotations is considered a String literal.

‘println’ = prints output for users to see and goes to a new/separate line.

‘print’ = prints output for users on the same line.

7.2: Variables and Data Types

In addition to printing, these data types can also be stored in variables.

Variable = A box with a name that can hold a value. They all have a name,
type, and value. To use a variable, it needs to be declared.

Declaring a Variable:

- Type of data
- Name of the Variable
Declaration = Data type + Variable Name

- Variable names are case sensitive

Once a variable is declared, it can be initialized (assigned a value).

Initialization = Data type + Variable Name + Value

- Data type in declaration must match assigned the value type.


- Variables can be initialized at the same time or after they are declared.
- Declaring a variable as ‘final’ prevents it from being altered.

- Difference between ‘String’ and ‘char’ is that char is a primitive data


type whereas String is a reference data type.
- ‘Primitive’ = Building blocks of programming languages.
- ‘Reference’ = Created by programmers using primitive types.
- When primitives are given values, they are associated with those
values. If the value of 1 variable changes, then the primitive value that
it stores changes too.

7.3: Expressions and Assignment Statements

- Modulus (%) = Finds Remainder of a division problem.


- Java program follows PEMDAS
- Variables can be initialized as complex expressions, storing the literal
value.

Literal Value = Written exactly as it’s meant to be interpreted (represented


by a primitive data type).

‘int’ division and ‘double’ division are not the same. Since ‘int’ values can’t
have decimal points, the results get truncated as the result has to be an
integer.

Truncated = The value of the division expression will get rounded down / The
decimal part will be excluded or taken away.

- Mixed division between a double and int will always be a decimal


value.
- Doubles dividing each other will always be a decimal value.

Using int, dividing by 0 will cause as ‘ArithmeticException’.

‘ArithmeticException’ = Unwanted / unexpected events occurring during


execution of a program.

- Using double will give a value of infinity instead of an error.


- A [+] between int’s will be addition, but if there is a string variable
added with the problem, then the int’s won’t be added, instead they
will be stacked.

Ex: x = 10, y = 12

[Link](x + y);  22

[Link](“Hello” + x + y);  Hello1012

[Link](“Hello” + (x + y);  Hello22

7.4: Compound Assignment Operations

Variables can store the outcome of expressions (equations). They can also
use their own existing value to change the value of a variable.

In programming, is common to add 1 to a variable as programmers use


counters to determine how many times a program / method has run.

- Shortcuts in arithmetic operations:

In Java, the = sign in a statement means an assignment of the right-hand


side value to the left-hand side variable.
7.5: User Input

An important part of Computer Science is creating an interactive program for


users.
‘Import [Link]’ = Imports class Scanner from ‘[Link]‘ for the
program.

‘[Link]’ = A package that stores various classes of code used to add


functionality to the program.

- Package = Used to group code into a folder for easy use.

Scanner = Class in [Link] which helps contain code specifically designed to


help user input / allows user input.

‘Scanner input = new Scanner([Link])’ =

- ‘Scanner’ = Data type


- ‘input’ = Variable name; can be anything
- ‘new Scanner’ = Creates a new scanner object
- ‘([Link])’ = Argument / Actual Parameter inputted into the
Scanner
o ‘[Link]’ is a value that typically corresponds to keyboard
input, and including this allows us to input keyboard data into our
program.
o Belongs to same class as ‘[Link]’.

Argument / Actual Parameter = Variable or value used in a given program.

In order to receive input from the user, a new variable must be created:

(Assigned value of the ‘Scanner’ variable).next(data type)

- Each data type has its own corresponding ‘next’ command that allows
the program to receive the input value.
- The data type the variable is declared as must correspond to the input
type.
- Using nextLine after nextInt / nextDouble leads to issues in the input
the program is receiving.
o When nextLine is called after a nextInt / nextDouble, input is still
reading input from the same line that the nextInt call came from.

User Input for Age:

- The user types 13 and hits Enter.


- nextInt() reads the 13 from the input. However, when the user hits
Enter, a newline character (\n) is also generated and remains in the
input buffer.

What’s Left in the Buffer:

- After reading the integer, the buffer still contains the newline character
(\n). So, the buffer now looks like this: \n.

Reading the Name:

- Now, when nextLine() is called, it immediately reads the newline


character (\n) that’s left in the buffer, not waiting for you to type a
name.
- As a result, name becomes an empty string because nextLine() thinks
that the input was just a newline (which is effectively nothing).

Printing the Name:

- When you try to print name, it prints nothing (a blank line) because
name is an empty string.

- This issue can be fixed using a buffer ‘[var].nextLine()’ statement that


absorbs the blank space following the ‘nextInt’ input.
- Afterwards, a new ‘nextLine’ input will be able to receive user input.

7.6: Casting and Range of Variables

Casting = Changing data types / turning something of one type into another
type.

- Done by adding the type you want in parenthesis to cast to that type.

When assigning a ‘double’ variable


to an ‘int’, an error occurs. However,
this can be fixed by adding ‘(int)’ to
the double variable when initializing
it as an int.
However, the same doesn’t apply
when casting an ‘int’ to a ‘double’
variable. This works through a
process called ‘implicit casting’.

Implicit casting = When java automatically casts the value correctly without
any programmer intervention.

- Casting is important when considering changes in numerical values


during division such as int’s being truncated when the result isn’t a
whole number.
- Casting an int variable as a double using ‘(double)’ in a division
problem will provide the correct result.

Casting can also be used to


round double values to the
closest int value. This is done
by using:

(int)(x + 0.5)

This ensures that the value of


[x] will be closest to the correct int.

This technique can also be used to round negative doubles by using (int)(x –
0.5).

Range of variables:

- In Java, an int is represented by 32 bits which means the total # of


possible combinations is more than 4 billion.
- The number system in Java is limited as there is a finite # of int
storage values.
- Because of this, there is a restricted amount of numbers usable in Java.
- The most optimal range is -2,147,483,648  2,147,483,647 as it’s split
between positive and negative numbers.

Integer limits in Java:


Overflow = Since the number system is limited due to storage constraints,
when going past this limit, the value is wrapped and proceeds back to its
max value.

Overflow doesn’t always work as intended and can create unexpected


results.

You might also like