Java Basics Flashcards
The way Java works
- Source
- Compiler
- Output (Bytecode)
- Virtual Machines (JVM)
What goes in source file?
Class definition
What goes in a class?
Methods
What goes in a method?
A set of statements, and for now you can think of a method kind of like a function or procedure
Why does everything have to be in class?
Java is object-oriented, thus each object represents a set of blueprint
Do I have to put a main in every class I write?
Nope. You might use a dozens of classes, but only one main method to evoke the program
What is the difference between a class and an object?
A class is blueprint for an object.
A object is like one entry in your address book
What if I need global variables and method? How do I do that if everything has to go in a class?
There isn’t a concept of ‘global’ variables and methods in a Java OO program.
What is a Java program? What do you actually deliver?
A Java program is a pile of classes. In a Java application, one of the classes must have a main method, used to start-up the program
How is this object oriented if you can still make global functions and global data?
Everything in Java goes in a class. So the constant for pi and the method for random(), although both public and static, are defined within the Math class
Object Oriented Programming
- Flexibility
2. Extensibility
Instance Variable
The object’s state (the data), and can have unique values for each object of the type
Class
A blueprint that tells the virtual machine how to make an object of that particular type
Main
- To test your real class
2. To launch/start your Java application
Garbage-Collectible Heap
- When the object is created, Java allocates memory space on the heap according to how much that particular object needs
- When the object never in use anymore, then that object becomes eligible for garbage collection
Primitive type
Hold fundamental values including integers, booleans, and floating point numbers
Object reference
- Hold bits that represent a way to access an object.
2. Hold an address of the object
Does all object references are the same size, regardless of the size of the actual objects to which they refer?
Yes. All references for a give JVM will be the same size regardless of the objects they reference, but teach JVM might have a different way of representing references on one JVM maybe smaller or larger than references on another JVM
Can I do arithmetic on a reference variable, increment it, you know - C stuff?
No. Java is different from C
Array
Arrays are always objects, whether they’re declared to hold primitives or object references
How do Java pass its parameters?
Java is pass-by-value, meaning everything is a copy
Can a method declare multiple return values? Or is there some way to return more than one value?
Sort of, A method can return an array
Do I have to return the exact type I declared?
You can return anything that can be implicitly promoted to that type. So, you can pass a byte where an int is expected. The caller won’t care, because the byte fits just fine into the int the caller will use for assigning the result.
You must use an explicit cast when the declared type is smaller than what you’re trying to return
Getter
A getter’s sole purpose in life is to send back, as a return value, the value of whatever it is that particular Getter is supposed to be Getting
Instance variables
- Always get a default value
2. If no assignment for instance variable, then it still has a value
Local variable
Do not get a default value. The compiler complains if you try to use a local variable before the variable is initialized
Comparing Variable
- Use == to compare two primitives
2. Use the equals() method to see if two different objects are equal