Comp 110 Vocabulary Definitions

Argument

The value that is passed into a method. 
Ex.  In:  "this.add(5,4)"  5 and 4 are arguments.

Boolean expression

Boolean expressions are statements that use boolean operators (Ex. &&, ||)

For example:

int x = 5;
int y = 2;
System.out.println(x==5);
System.out.println(x==3);
System.out.println(y==2 && x==5);
//Output:   Prints: true, false, true. 

In the above example, x==5, x==3, and (y==2 && x==5) are all boolean expressions.

Class

A class is the filename for your source code, as well as an identifier for your Objects

Ex:

package comp110;
public class Person {
    int _age;
    String _name;
    public Person(int age, int name){
        _age = age;
        _name = name;
    }
    //methods here
}

A class consists of two parts: the visibility and the classname. In the above example, our visibility is public and our name is "Person". In addition, The classname must be the same name as your filename. In the above example, the Person class is saved as "Person.java". 

More information here: https://docs.oracle.com/javase...

Concatenation

This happens when you "add" two Strings together. It means, simply, to join one String to another. When the program concatenates two strings, the result is a single String value. You can also concatenate numerical values, which can be very helpful!

For example:

int answer = 42;
String example = "The answer is " + answer;
//example's value is now "The answer is 42"

Constructor

constructor in Java is called when an instance of an object is created. Specifically, a constructor is a special type of method that is specifically used for creating objects. Some notes about constructors:

  • A constructor has no return type
  • The constructor's name must be the same as the classname
  • To call a constructor, the new keyword is called. 

Let's take a look at our Person example. 

package comp110;
public class Person {
    int _age;
    String _name;
    public Person(int age, int name){
        _age = age;
        _name = name;
    }
    //methods here
}

In this example, Person is the name of the constructor. In order for our constructors to be called from another class, they must be public as well. 

In order to call the constructor, one must go to a separate class and instantiate the Person class:

package comp110;
public class Runner{
    public static void main(String[] args){
    //Instantiates the Person class 
    Person var = new Person(21, "Connor");
    }
}

Expression

An expression is a construct made up of variables, operators, and method invocations, that evaluates to a single value. They are the fundamental building block in programs.

Some examples of expressions include:

  • Equations
  • Assigning variables to a value
  • Statements that use mathematical operators such as *, /, + and %
  • Statements that use boolean operators such as || and &&

Concrete examples include:

int x = 5;
int y = 2+3;
int c = x*4;
System.out.println(3+4); //expression inside of method call
boolean bar = (x==5 && y==5);
boolean foo = (c/20==1 || y==6);

There are two big ideas behind expressions

  1. Every expression simplifies to a single value.
  2. Anywhere you can write an expression, you can choose any expression you'd like as long. 

Getter

A getter is a method that retrieves a value stored in an instance. For example, the Color class in JavaFx has a number of values associated with the instance, specifically Red, Green, and Blue values. We can use getters to retrieve these values. 

//Color declaration is: Color(double red, double green, double blue)
Color c = new Color(0.5, 0.4, 0.3);
System.out.println(c.getRed()); //prints 0.5
System.out.println(c.getGreen() + c.getBlue()); //prints 0.7

Below is another example:

public class Person{
private int _age = -1;
    public int getAge(){
        return _age;
    }
    public void setAge(int newAge){
        _age = newAge;
    }
}

Initialization

Initialization means to set an initial value to an instance or object. For example, the below Color "face" is not initialized, it is only declared.

Color face;
System.out.println(face); //prints out null 

In order to initialize it, we have to give it a value. Here we are instaniating a new Color object, and initializing "face". 

Color face = new Color(0.4, 0.3, 0.6);
System.out.println(face); //does not print out null, is initialized


Instance variable

Instance variables are variables that exist in a class, but are not associated with a specific method. For example, "_age" is the instance variable in the class below. 

public class Person{
private int _age = -1;
    public int getAge(){
        return _age;
    }
    public void setAge(int newAge){
        _age = newAge;
    }
}

By convention, we put an "_" infront of the variable name for instance variables. Java will still compile if you do not have an underscore, but for the purposes of this class all of our instance variables will have an underscore to denote them as an instance variable. 

Instantiation

Instantiation is distinctly different than initialization. Instantiation means to only create the object. For example, the below line instantiates and return a new Color object: 

return new Color(0.5, 0.6, 0.8);

Interface

An interface is a list of methods. If a class "implements" an interface, then that class must implement those methods. Take the follow interface below as an example. 

interface Bicycle {
    //  wheel revolutions per minute
    void changeCadence(int newValue);
    void changeGear(int newValue);
    void speedUp(int increment);
    void applyBrakes(int decrement);
}

Below is a class that implements this interface. 

class ACMEBicycle implements Bicycle {
    int cadence = 0;
    int speed = 0;
    int gear = 1;
   // The compiler will now require that methods
   // changeCadence, changeGear, speedUp, and applyBrakes
   // all be implemented. Compilation will fail if those
   // methods are missing from this class.
    void changeCadence(int newValue) {
         cadence = newValue;
    }
    void changeGear(int newValue) {
         gear = newValue;
    }
    void speedUp(int increment) {
         speed = speed + increment;   
    }
    void applyBrakes(int decrement) {
         speed = speed - decrement;
    }
    void printStates() {
         System.out.println("cadence:" +
             cadence + " speed:" + 
             speed + " gear:" + gear);
    }
}

Method

A way to package code, so you can call it multiple times. 

<visibility> <return type> methodName(<Type> parameter1, <Type> parameter2) {
    //do the calculation here
}

Two concrete examples:

public void printSum(int a, int b){
    System.out.println(a + b);
}

Method Call

A method call is used to execute the code in a method. Below is a few ways to call methods using the methods we defined above. 

//Creates a color with a red, green and blue value. 
Color c = new Color(0.5, 0.8, 0.9); 
System.out.println(c.getRed()); //prints out the vale of red. 

c.getRed() is a method call. All method calls require parenthesis after the name of the method. 

Method Signature

A method signature is the visibility, return type, method name, and parameter list in that order. For example, for the "printSum" method defined under the "Method" definition listed above, the signature is:

public void printSum(int a, int b);

Notice that the signature is everything that the method has, except it is missing its "guts".

new 

The new keyword is used to create instances. For an example, see the Getter definition.

Object / Instance

An Object is a type of variable that is:

  1. Defined by the user, it is not a standard Java type. 
  2. Initialized by the new keyword.
  3. Can be used to package fields, which are variables associated with an object (inside of them).

rectOne, below, is a Rectangle object with a width and height. 

Rectangle rectOne = new Rectangle(100, 200);

The Rectangle class looks something like this:

public class Rectangle{
    private int _width = 0;
    private int _height = 0;
    
    public Rectangle(int width, int height){
        _width = width;
        _height = height;
    }
//other methods defined here. 
}

Notice the instance variables _width and _height. These are the field in our object. They are packaged inside of "rectOne"


Operator 

An operator is a special character that performs a calculation or comparison between two types. Mathematical operators include : +    -    /   and  %. Boolean operators include: >     <      <=     >=    !=  and   == 

Example:

int i = 5;
int j = 4;
System.out.println( i < 8 ); //Since this is a true statement, this prints out true
System.out.println( 4 + 8 ); //Prints 12
System.out.println(j < 4); //j < 4 is a false statement, (4 is not less than 4) Prints false

Return Types

A return type is a type specified at the time of method creation, which defines what a method must return before the end of its code. A return type can be any data type (int, boolean, double, etc), and it can also be of any value type(String, double[][], Rectangle). The return statement for a method must return a variable of the same type. Ex:

public String concatenateAge(int age){
    return "My age is: " + age;
}

This method has a return type of String, which means that the "return" keyword must be followed by a variable of type String

Parameter

Parameters are local variables that are declared in the method signature, which, when the method is called, must be supplied arguments in order to call the method. For example:

public int multiplyNumbers(int x, int y){//these are the parameters
  return x * y;
}
public static void main(String[] args){
  System.out.println(multiplyNumbers(3, 5)); //prints out 15
}

Setter

Setters are void methods which sets a value inside of an instance. This is done by providing an argument to the method, which then takes a value and changes it inside the instance. For example, let us say we have these two methods inside of a Person class.

public class Person{
private int _age = -1;
    public int getAge(){
        return _age;
    }
    public void setAge(int newAge){
        _age = newAge;
    }
}

The setter above is setAge. Notice that it is a void method. This means it doesn't return anything. It takes in a new value called newAge and it sets _age to it. This is what we call setting the value of the instance variable. Below is how we'd use it.

Person john = new Person();
System.out.println(john.getAge()); //prints -1
john.setAge(45);
System.out.println(john.getAge()); //prints 45

We can set many things, including ints, doubles, and other types.

Statements

Statements are equivalent to an English sentence, except they usually end with a semi colon instead of a period. Each statement is an instruction for the computer. 

An example of a statement is:

System.out.print("Hello World!");

Types

type is the word that describes what a variable is. Types include integers, words, decimal numbers, and booleans (true or false values). 

There are four simple data types:

  • Numerical:
    • int: whole numbers, ideal for counting
    • double: percentages, ideal for fractional amounts and more precise numbers
  • Textual:
    • String: textual information, good for words and sentences, a "string" of characters
    • char: short for "character", textual information, represents single letters, rarely used because String tends to be more useful for textual data 
  • Boolean: true or false values
  • Reference to a complex data type

There are two categories of complex data types composed of simple data types:

  • Arrays are made up of a series of values of a single type
  • Objects can be made up of many values of many types 

respectively.

There are 

In Java when you declare a variable, the type must be put before the name of a variable. Ex:

int x = 5*2+6/2;
String name = "John";

Variable

A variable is a name in java that stores a value. Behind the scenes, it represents a location in your computer's Random Access Memory, or RAM. When you declare and assign a variable, you are storing a value into your computer's RAM... cool!

A few examples:

int variable = 1;
double variableTwo = 3.3;

 In the above example, the variables are variable and variableTwo

Java Documentation: https://docs.oracle.com/javase...

Visibility

Visibility is an attribute of methods, classes, and variables which defines the scope of that method, class or variables. There are four different types of scopes, or visibilities:

  • public: Most of our methods and classes are marked as public. This is so that every program can call these methods, or instantiate new objects, and we do not have to worry about permissions. As for programming practices, it is bad to mark everything as public, as there are some pieces of data that you do not want publicized or able to be changed.
  • protected: Protected items are available for access within the class, within the package and also by objects that are subclasses of the class where the method is defined.
  • default / no modifier: Default or no modifier methods/classes/variables are accessible within the class, and within the package.
  • private: This is what our instance variables are marked as. We set our instance variables as private so they cannot be directly modified or accessed, instead we use public getters and setters in order to provide the information outside of the class.

Take a look at this handy chart for an easier representation: