Lecture 5 Flashcards
Java and Python difference in storing objects.
Java stores some values on the heap as objects.
Python stores everything.
Java and Python difference in values not stored on the heap.
Java stores them at bit-sequences.
Python does not do this.
Java and Python difference in Type information.
Java has type information specified in the program (at compile time).
Python stores type information in the object itself (at run time).
Numeric types, Java vs. Python.
Java: Bit sequences.
Python: Objects.
Integer range. Java vs. Python.
Java: Limited range.
Python: Not limited.
Types of integers. Java vs. Python.
Java: Multiple types.
Python: One type.
Types of floats. Java vs. Python.
Java: Multiple types.
Python: One type.
Float ranges. Java vs. Python.
Java: Different types, different ranges.
Python: One type, one limited range.
Numeric types in Java.
int
byte (8 bits)
short, a kind of int
long, a kind of int
double, double* precision float.
float
char, represents code units in Unicode
Usage advice of number types for CMPT270.
Int for counted quantities.
Double for measured.
Others for special purposes only. Don’t worry about them too much.
Note: Java’s numeric types are very similar to C/C++.
Java arithmetic operators.
Same as Python. +,-,*,/,%
int operated with int leads to int
float operated with float leads to float
mixed types operated on leads to implicit conversion to a floating point
% is restricted to integers only
Numeric expressions. Java vs. Python.
Java: Results in bit sequences that can be copied somewhere.
Python: Makes a new object.
Python’s way results in extra overhead and takes a bit longer.
Booleans in java.
boolean: true, false
char in Java.
Literals written with single quotes. Any single letter, number, keyboard symbol.
Strings in Java.
Any sequence of characters. 0 or more.
Written with double quotes.
Strings are a class*, not a primitive.
Boolean expressions in Java.
&&
||
!
These are from C.
Java rational expressions.
==
>=
etc…
Inherited from C.
Where is data for primitive types stored in Java?
On the stack.
How do reference types work with memory?
The reference type in the stack holds a pointer that leads to an object on the heap.
Java. What happens when you set a reference type variable to another?
A copy of the pointer is made. Like in Python?
Java declaring variables.
tell the compiler the name and type.
type name;
The compiler allocates exactly enough memory for the type.
Every variable must be declared before it can be used.
Java. What is initializing a variable?
The first time a variable is assigned to something.
How many times can a variable be declared and initialized?
One once for any variable. Although new values can be assigned.
Constants in Java.
Use final keyword.
i.e.
final type name;
These can be useful for adding clarity to calculations.
final tells the compiler that no future changes are allowed.
Type casting in Java.
Not allowed implicitly.
I.e.
double exampleDouble = 9.8;
int exampleInt = (int) exampleDouble;
exampleInt is 9
Type casting double to int truncates, not rounds.
Scope in Java.
{} shows the structure.
A variable’s scope is it’s lifetime. They are not usable outside it’s scope.
Java. Conditional statements.
if (condition)
{
stuff;
}
else if (condition)
{
stuff;
}
else
{
stuff;
}
Note the “else if” instead of “elif” like in python.
Java while loops.
while (condition)
{
stuff;
}
Java for loop.
Example:
for (int i = 0; i<10; i++)
{
stuff;
}
Java. Do while loops.
do
{
stuff;
} while (condition);
Console output in java.
Printing uses System.out object.
System.out.print(); has no newline
System.out.println(); has a newline.
Java. How to get string length?
.length() method
Java. String concatenation.
string1 + string2
This also works with other data types.