Java language basics Flashcards
Data types specify….
specify the kind of thing a value represents
Variables are….
used to store values for later use. must have a specified data type
Arrays are….
used to store multiple values in a single variable which all must be the same data type
Conditional Statements allow…..
Allow for branching and looping in Java
entry point for the program to begin
main method - every java program must have one
main method syntax
public static void main (String[] args) {
//code goes here
}
Class names should be _________ case?
PascalCase
One of the Class names much match the?
filename.java
Sequence Diagrams show…..
show how application components or separate applications interact with each other and in the order of interactions.
Sequence Diagrams are structured…..
are structured to represent a timeline beginning at the top and descending to depict the sequence of interactions.
Class diagrams are used…..
are used to describe classes and their relationships
Class diagrams show….
the names and attributes of the classes, relationships between the classes, and sometimes also the methods of the classes.
What do the plus and minus signs indicate in this class diagram
+ before the attribute name means the attribute is public
- means the attribute is private so it is not visible to outside sources
What do the words following the colons represent
In a class diagram this item when following attributes, it’s the data type for the attribute. When following methods, it’s the return type for the methods.
What is the class name?
Person
What data type does setName return?
None the void means it doesn’t return anything
What are the items below the second line?
This row of the diagram is a section containing the Constructors and Methods. Constructors are listed first and then methods.
What are the items at the top of this sequence diagram
the customer object who is outside our diagram and the theater and server objects inside our diagram
What is the vertical dashed line?
A lifeline that represents the different objects or parts that interact over time.
What are the solid horizontal lines?
Messages being sent between objects
What are the dashed horizontal lines?
Direct response messages that are the return for a message that was sent.
What is the vertical rectangles/activation boxes in some sequence diagrams?
The Activation box is present under the object next to the code for each process which the object is involved.
what is casting?
The conversion of one data type to another
what is the memory size order from smallest to biggest of these data types:
int
float
byte
double
long
short
byte < short < int < long < float < double
what is the syntax for casting a larger data type (float) into a smaller(int) (Narrowing Casting)
float floatVar = 45.67
int intVar = int(floatVar)
What is the size and range of values of a byte?
byte | 8-bit | -128 to 127
What is the size and range of values of a short?
short | 16-bit | -32,768 to 32,767
What is the size and range of values of a int?
int | 16-bit | -2 billion to 2 billion
What is the size and range of values of a long?
long | 32-bit| -9 quintillion to 9 quintillion
What is the size and range of values of a float?
float | 32-bit | 6 decimal digits
What is the size and range of values of a double?
double | 64-bit | 15 decimal digits
What is the size and range of values of a char?
char | 16-bit |single character
What is the size and range of values of a boolean?
boolean | 1-bit | true or false
What is a wrapper class?
A corresponding class to the primitive data types that allow us to define the primitive data type using that class instead.
what is the Java.lang package?
A package that loads automatically with Java.
What are the hundreds of included classes in the java language called?
Packages
What separator do java package paths use?
Unlike folders, Java package paths use dots (.) as separators instead of slashes (\ or /).
What java package is imported by default and contains built-in classes like String?
Java.lang (lang is short for language)
When you want to use a class that’s part of a package what the common way?
The common way is to add an import statement to your class file where the other class will be used. This allows you to only use the class name in your code versus the full package name.
What will this line of code do? import java.util.*;
It will import all of the classes in the java.util package for use.
Why is code line a) preferred over b) when we only need the Scanner class?
a) import java.util.Scanner;
b) import java.util.*;
Importing only what you need will lead to improved maintainability, the readability of your code, and you won’t have problems with ambiguous class names (2 classes with the same name in 2 different packages).
Where do the lines importing packages/classes go in your code?
At the very top.
What is the difference between ++i and i++?
++i increases the value of i by 1 before executing the code on the line.
i++ increases the value of i by 1 after executing the code on the line.
What do the dots represent when referring to packages?
The dots represent subdirectories.
Why should we list the specific class to import and not use the star import (*)?
Better readability
Reduced errors
What is a jar file?
A jar file is like a zip file that contains complied code. Java can search inside jar files and use the classes included, without expanding the archive on the disk.
What is reference equality?
When two references point to the same object.
What is the difference between equality and equivalence when comparing objects?
Equality check is the objects point to the same memory location. Equivalence checks to see if the object have the same variables and data.
what is the syntax to compare stringOne and stringTwo?
stringOne.equals(stringTwo);
What is the syntax to check if one object is equal to another?
objectOne == objectTwo
This only returns true if both of the objects are assigned to the same object.
What is a simple explanation of a hashCode?
It is an integer used to quickly search for objects on Java.
When you override the equals method you also need to override the……..
When you override the equals method, you also need to override the hashCode method.
What does overriding the “equals” method do?
I allows us to establish equivalence between two objects. That way the “equals” method will return true even if == returns false, meaning you are dealing with two separate, but equivalent, objects.
What are the six steps to overriding .equals() for the class City?
Check for self-reference: Check if the object being compared is the same instance as this object. If it is, return true, since an object is always equal to itself.
Check for null: Begin by checking if the object being compared is null. If it is, return false, since a null object cannot be equal to any other object.
Check for class compatibility: Check if the object being compared is an instance of the same class as this object. If it is not, return false, since objects of different classes cannot be equal.
Cast the object: If the object being compared is an instance of the same class, cast it to the appropriate type.
Compare object fields: Compare the fields of the object being compared to the fields of this object. Use the appropriate comparison method for each field type (for example, == for primitive types, and equals() for objects).
Return the result: Return true if all fields are equal, or false otherwise.
Use this card to memorize the syntax for overriding the equals method in a class
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
if (getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age && name.equals(person.name);
Create the equals method that does the following:
1) Take in an Object class argument and call it spare.
2) Check if the current object and the spare object are the same object by returning true if the current object (this) is equal to (using ==) as other.
3) Check if the spare object is null by returning false if other is null.
4) Check if the spare object is of the same class as the current object by returning false if getClass() is not equal to (using !=) to spare.getClass();.
5) Check if the spare object’s fullName and trueAge attributes are the same as the current objects by:
First casting the spare object into a Person class.
Comparing if the current objects fullName and trueAge are equal to the cast object’s fullName and trueAge.
@Override
public boolean equals(Object spare) {
if (this == spare) return true;
if (spare == null) return false;
if (getClass() != spare.getClass()) return false;
Person sparePerson = (Person) spare ;
return fullName.equals(sparePerson.fullName) && trueAge == sparePerson.trueAge);
OR
@Override
public boolean equals(Object spare) {
if (this == spare) return true;
if ((spare == null) || (getClass() != spare.getClass()))
return false;
Person sparePerson = (Person) spare ;
return fullName.equals(sparePerson.fullName) && trueAge == sparePerson.trueAge);
a and b are two objects, and a.equals(b) returns true. Also, a.hashCode() returns 1234, what will be returned by b.hashCode()? Assume that both implement equals and hashCode methods correctly.
1234 – Equal objects have equal hash codes. Since a and b are equal, and the hash code of a is 1234, the hash code of b will also be 1234.
What will happen when you compile and run the following code?
String s1 = “Manish”;
String s2 = s1;
s1 = null;
System.out.println(s2.equals(s1));
false – Whenever null is passed as an argument to the equals method which is invoked on a non-null reference; it always returns false. In this code, s2 refers to the String “Manish”, and s1 becomes null. Hence the call s2.equals(s1) returns false.
What will be printed when the following code is compiled is run? Type only the output that will printed in correct case without any quotes or spaces.
Object i = new Integer(7);
Object f = new Float(7);
Object d = new Double(7);
System.out.println(i.equals(f) == d.equals(f));
true – All the wrapper classes return false if the argument of the equals method is of different type. i.e. In the case of all wrapper classes, a.equals(b) will return false if a and b are objects of different classes, irrespective of the value they represent. Here, the calls i.equals(f) and d.equals(f) both return false, and comparing two false with an equality operator (==) prints true.
What is the code for importing Java’s built in hashCode method?
import java.util.Objects;
What is the code to use the variables name and age to override the hashCode using Objects.hash?
public int hashCode() {
return Objects.hash(name, age);
}
What’s the first if statement code inside the method: public boolean equals(Object o) {
}
if (o == this) {
return true;
}
What’s the 2nd if statement code inside the method: public boolean equals(Object o) {
}
if (o == null) {
return false;
}
What’s the 3rd if statement code inside the method: public boolean equals(Object o) {
} (note the class is Person)
if (!(o instanceof Person)) {
return false;
}
What’s the last statement code inside the method: public boolean equals(Object o) {
} (note the class is Person with the variables name and age)
Person other = (Person) o;
return this.name.equals(other.name) && this.age == other.age;
}
What does the .equals() method compare by default?
By default, the equals method in Java compares the memory addresses of two objects to determine if they are equal. If two objects are of different classes, they will have different memory addresses and therefore equals will return false.
What’s wrong with this initialization: float myfloat = 3.14;
An f needs to be added: float myfloat = 3.14f;
When you initialize a float variable with a floating-point literal, the compiler needs to know whether the value should be interpreted as a float or a double. If not the line with cause an error.
What’s the default value of a boolean?
false
What’s the default value of a char?
‘\u0000’ (the unicode representation of the ASCII code for null)
What’s the default value of a String?
null
What are the naming restrictions on variable names?
Cannot contain whitespace
Cannot start with a digit
Cannot be a Java reserved word (Ex. data type names)
Needs to be unique within its scope (ex: only one declared with this name in a class or method)
With Arrays if you don’t initialize it at the same time as the declaration what must the declaration statement contain?
new :
int[] numbers;
numbers = new int[5];
or
int[] numbers;
numbers = new int[]{1, 5, 6, 7};
What is a use case?
A use case in programming refers to a description of how a user or an external system interacts with a software system to achieve a particular goal or task. It is a technique used in software development to identify, analyze, and document the requirements of a software system from a user’s perspective.
What is composition?
Composition is a fundamental concept in object-oriented programming (OOP) where a class is made up of one or more objects of other classes. It allows creating complex types by combining simpler types, and it is used to build more complex objects by combining simpler objects.
In composition, the objects that are part of a class are called its “components” or “parts”, and they are typically stored as private member variables of the containing class. The containing class can then use the methods of its components to provide its own functionality. This allows for code reuse, as the same components can be used in different classes to provide similar functionality.
does the java compiler require each public class to be it’s own file or in a shared file?
In Java, each public class must be defined in its own file with the name of the file matching the name of the class. However, you can have multiple non-public classes defined in the same file as the public class.
The Java compiler requires that if a class is in a package, and if it uses any import statements, they must be in the order of:
Package declaration (if the class belongs to a package)
Import statements
Class declaration
will .* import all the packages and child packages?
No, .* does not include the child packages
What’s the method signature used to override the .equals method?
public boolean equals(Object o) {
}