Basics_Java Flashcards
Where do we put parameters for a function?
we use parameters to pass values to function
Where do we put the methods for our functions?
Inside the curly braces.
In Java the curly brace is on line with function name and return type
what is a good function (method name)?
clearly identifies the purpose of the function
camelCase notation
What is a function?
A block of code that performs a task
Think of buttons on a remote
In Java, functions are the smallest building blocks
Ex. functions to validate user info, convert weight to kgs
What is a package?
A concept for grouping related classes.
We will have hundreds or thousands of classes.
We should organize these classes into packages.
Java creates a namespace for our classes:
com.namespace
*every class we create in our program will belong to this package
Where do we specify a return type?
A function is a block of code that performs some task.
We specify the return type before the name.
Some functions don’t return anything.
Where is the entry point into our program?
Every Java program must have one function.
The main function is executed first.
What is a class?
A container for related functions.
We use classes to organize our code.
Ex. The supermarket has related products in different “classes” or sections.
What class must every Java program have?
Every program in Java has a main class that contains the main function.
What is a method?
A method is a function that is part of a class
What is an access modifier?
A special keyword that determines if other classes and methods can access these classes and methods.
Private
Public
What is the naming convention for classes and methods?
What are the two steps involved in running a Java program?
How do we get byte code?
Java Compiler changes our source code into byte code
Who developed Java?
At Sun Microsystems.
Sun Microsystems was later acquired by Oracle in 2010.
It was originally called Oak named after the tree outside Goslings office.
Where can our Java byte code run?
Java byte code is platform-independent
It can run on Linux, mac, windows or any operating systems that have a Java Run-Time Environment
What is java byte code?
The instruction set for the Java Virtual Machine
Assembled code is runnable on a CPU with a specific instruction set, while bytecode can be executed in a virtual machine (such as the Java runtime) on any CPU that can run the VM. Assembly code is (represents) the native code for the processor you are programming.
What is a Java Run Time Environment?
The Java Runtime environment has a component called Java Virtual Machine (JVM)
JVM translates our byte code into the native executable language for various platforms (Mac, Windows, Linux)
What is byte code?
What does the Java Virtual Machine (JVM) perform?
Converts our Java Byte code into the native code for the operating system we are using like a mac, windows or linux operating system.
This makes applications portable.
Can execute on Linux, Mac or any other operating systems that have a Java Run-Time Environment.
Who owns Java?
Oracle
Oracle Corporation is the current owner of the official implementation of the Java SE platform, following their acquisition of Sun Microsystems on January 27, 2010.
How many editions are there of Java?
Four Editions for different types of applications
SE - all the libraries all Java developers must learn
EE - provides additional libraries for building fault-tolerant multi distributed systems
ME - libraries specific for mobile phones
Java Card - for smart cards
How many worldwide developers does Java have?
9 million developers world wide
How many mobile phones run Java?
3 billion mobile phones currently run Java
What is JDK?
Java Development Kit
Software Development Environment
Compiler
Reusable Code
Java Run Time Environment
How many TV sets run Java?
120 Million TV sets
Every blue-ray device
What is JVM?
The JVM has two primary functions:
to allow Java programs to run on any device or operating system (known as the “Write once, run anywhere” principle), and
to manage and optimize program memory.
When Java was released in 1995, all computer programs were written to a specific operating system, and program memory was managed by the software developer. So the JVM was a revelation.
What is the average US salary for a Java developer?
$101,929 USD
Java is everywhere.
There are many opportunities to get hired as a professional Java developer.
What topics does this course include?
Build clean code
Fix bugs and errors
Understand types
Create algorithms
Package and deploy programs so other people can use them!
What’s included in types?
Variables and Constants
Primitive and Reference Types
Casting
Numbers, Strings and Arrays
Reading Input
What is a variable?
A variable is how we temporarily store data in computer memory.
What is an identifier?
The name of a variable
”=” is assignment operator
What can we do with variables?
Copy the value of one into another
What is the naming convention for variables?
Camel case!
lowercase first letter Uppercase other letters!
Ex. int herAge = 30
What are the two types in Java?
What are the primitive types in Java?
int - allow us to store values up to 2 billion
long - store numbers greater than 2 billion
double - storing values with decimal values
float - storing values decimal values
char - storing characters
boolean - store true or false
What is best practice for variables?
always use meaningful, descriptive names
What must we do to a long type in Java?
add and L after the value.
By default Java compiler recognizes a number as an integer.
Put an L after to tell Java compiler its a long!
What must we do to use a float type?
Java see’s numbers with decimals as doubles by default.
We have to add a suffix to represent the number as a float
Add “F”
How do we initialize a char type?
surround single characters by single quotes
string represents a series of characters
What are reference types?
objects + their members (data/functions)
What is casting?
Type conversion
How do we initialize reference types (objects)?
use the new operator
Allocate memory
How will Java execute this statement?
First, allocate some memory to store the point object
Next, allocate a separate part in memory to store a reference (the address) to the point object
Finally, assign this reference to the variable name
How are primitive types stored in memory?
Value is stored in the variable at that memory location
x and y are completely independent of each other
What are we copying into pointTwo in this expression?
The reference to pointOne object in memory, not the point object
What is the crucial difference between how primitive and reference types are stored in memory?
When we declare a primitive variable like a byte the value we assign is stored in that memory location.
When we use a reference type our variable holds the reference (address) of the object in memory not the actual value.
How does Java store reference types in memory?
Variable holds the address or reference of the object in memory, not the actual value.
Two variables can reference the same object.
What should we remember about the differences between primitive and reference types?
Remember,
reference types are copied by references.
primitives are copied by values and are completed independent of each other.
What are strings in Java?
Strings are reference types.
We use a short way to create them (can omit the new operator)
Strings are a class so we can access methods with the dot operator!
What can we do with strings?
because strings are a class,
we can access methods like ends with, length, indexOf(), replace etc.
Ex. check length of user input string
Ex. give the index of the first character
What is the difference between parameters and arguments?
parameters are the holes we define in our methods.
arguments are actual values we pass to these methods.
What are strings in Java?
Immutable
We cannot mutate them, we cannot change them
any methods that modify a string create a new string object
What is a useful string method in Java?
the trim method
used if a user types unnecessary white spaces in form fields
What don’t primitive types have?
Members
properties (data) or functions
What is an object?
An instance of a class
A reference type
object/classes have members we can access using the dot operator
What type are arrays?
Reference types
We need the new operator
we can access elements or items in the array via an index
By default, when we print an array what does Java return?
Java returns the string of the address in memory
to see the actual items in the array we must use the array’s class
What is a shortcut for generating
System.out.println()
?
“sout” + Tab
How many bytes are used in primitive types?
How can we initialize string objects in Java?
String is a reference type however, we have a shortcut
Can omit the new operator
JavaRuntime will handle
How do we return the string representation of an array?
Use the array class .toString method
there is a .toString for every primitive type
What two categories of types in java?
What is one of the differences between the primitive and reference types in java?
Reference types use the new operator to allocate memory for the reference variable
Primitive types don’t require us to allocate memory java run time environment does this for us!
What are the four most common String escape sequences?
" = "fileNames"
\n = new line
**** = \
\t = tab
What is a modern syntax for initializing an array when you know the values ahead of time?
What do we use arrays to do?
Store a list of items:
numbers
people
message
Describe the Arrays class in Java?
Defined in Java.util
Has useful methods:
Arrays.toString
What can we create in Java?
Multi-dimensional array’s
Can create a 2D array to store a matrix
or a 3D array to store data for a cube
These are useful for scientific computations
Ex. two rows and three columns
What is the convention for naming constants?
all CAPS
add the final
What arithmetic operators do we have in Java?
All the same in math like “+” “-“ “*” “/”and “%” (modulus or remainder)
/ gives a whole number
What is method overloading?
In Java.util package
Arrays Class
Arrays.toString method - returns string rep of array
defined for all primitive types
What is an expression?
A piece of code that produces a value
How can we execute a division to produce a decimal result?
s are called operands
By default Java returns integer values for integer division.
Cast our input into double