Java Syntax

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/15

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 1:52 AM on 3/23/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

16 Terms

1
New cards

Provide the generic syntax for Interfaces

interface MyInterface {
	
	int VALUE = 10;                  // same as public static final int VALUE = 10;
	public static final int X = 20;  // explicit form

	void methodA();                  // public abstract
    	public abstract void methodB();  // explicit form

    	default void methodC() { }

	// private
	// static
}

2
New cards

Provide the generic syntax for Abstract Classes

abstract class MyClass {

	// Fields
	public int fieldA;
	// public, private
	// static, final
	
	//Constructor
	MyClass(){}
	// public, protected, private (only can be called in this class)
	
	// Methods Abstract Concrete
	public void methodA() { }
	// public, private
	// abstract, static, final	

}

3
New cards

Provide the generic syntax for Records

record MyRecord(int fieldA, String fieldB) {

	// Fields - Implicitly private final
	private final int x;
	private final String name;

	// Additional Fields - Must be static
	static int count = 0;
	// public, private
	// static, final


	// Canonical Constructor - Auto-generated, must be same visibility as record
	public MyRecord(int fieldA, String fieldB) {
        	this.fieldA = fieldA;
        	this.fieldB = fieldB;
    	}
	
	// Compact 
    	MyRecord {
        	if (fieldB == null) {
            	throw new NullPointerException();
        	}
    	}
	
	// Additional
    	public MyRecord(int fieldA) {
        	this(fieldA, "default");
    	}
	
	// Methods
	public void methodA() { }
	// public, private
	// static, final

	// Setter Method - returns new record
	public MyRecord setX(int newX) { return new MyRecord(newX, name); }

	//Getter Methods - auto-generated
	public int x() { return x; }

	public String name() { return name; }

4
New cards

Provide the generic main method syntax


public class Test {
    public static void main(String[] args) {
   
    }
}

5
New cards

Provide syntax for iterating through the values of a map

Map<String, Integer> mapName = Map.of(
    "A", 1,
    "B", 2,
    "C", 3
);

for (Integer value : mapName.values()) {
    System.out.println(value);
}

6
New cards

Provide syntax for iterating through the keys of a map

Map<String, Integer> mapName = Map.of(
    "A", 1,
    "B", 2,
    "C", 3
);

for (String key : mapName.keySet()) {
    System.out.println(key);
}

7
New cards

Provide syntax for iterating through the key-value pairs of a map

Map<String, Integer> mapName = Map.of(
    "A", 1,
    "B", 2,
    "C", 3
);

for (Map.Entry<String, Integer> entry : mapName.entrySet()) {
    System.out.println(entry.getKey() + " " + entry.getValue());
}

8
New cards

Provide syntax for the two basic methods of iterating through a list

List<String> listName = List.of("A", "B", "C");

for (int i = 0; i < listName.size(); i++) {
    System.out.println(listName.get(i));
}

for (String element : listName) {
    System.out.println(element);
}

9
New cards

Provide syntax for an unmodifiable list

var ns= List.of(1,2,3,4);
System.out.println(ns); //[1, 2, 3, 4]
ns.add(10);//fails -- unmodifiable collection! A useful kind of failure.

10
New cards

What is the toString() output for a record called Person

Person[name=Bob, friends=[Alice, Cara]]

11
New cards

What is the printed structure of a Map? If it includes objects?

{key1=value1, key2=value2, key3=value3}

{Point[x=0, y=0]=base, Point[x=0, y=1]=down, Point[x=1, y=0]=left}

12
New cards

Overriding + Recursion - Method Call Output

class Pow {
    public int pow(int base, int exp) {
        if (exp == 0) { return 1; }
        return base * this.pow(base, exp - 1);
    }
}

class PowLog extends Pow {
    public int pow(int base, int exp) {
        System.out.println("LogMessage");
        return super.pow(base, exp);
    }
}

System.out.println(new PowLog().pow(4, 3));
  • super.pow(...) starts in the superclass method

  • but inside that method, this.pow(...) still uses dynamic dispatch

  • so if the actual object is a PowLog, the recursive call goes back to PowLog.pow(...)

13
New cards

Give the syntax for forwarding a super

interface A{
	default String mA(){ return "A"; }
}

interface B extends A{
	default String mA(){ return "NotA"; }
	default String mB(){ return "B" + A.super.mA(); }
}

record C() implements B{ //'implements A, B' would behave identically
	public String mA(){ return "C" + B.super.mA(); }
}

System.out.println( new C().mA() ); //CNotA
System.out.println( new C().mB() ); //BA

14
New cards

The perfect case to use an interface

interface Length{
	default double meters(){ return yards() * 0.91d; }
	default double yards(){return meters() * 1.09d; }
	default Length add(Length l){
		return new Meters(l.meters() + this.meters());
	}
}

record Yards(double yards) implements Length{}

record Meters(double meters) implements Length{}
// Since records auto generate getters for the parameters, with the same name as the parameter, this new method overrides the method in the Length interface enabling less code.

15
New cards

The perfect case to use an abstract class

abstract class LibraryProperty{
	
	private final static List<LibraryProperty> all= new ArrayList<>();
	
	public LibraryProperty(){
		all.add(this);
	}

	public static List<LibraryProperty> inventory(){
		return Collections.unmodifiableList(all);
	}

}

class Book extends LibraryProperty{}
//new Book() will automatically add it to the list!
class Magazine extends LibraryProperty{}
//new Magazine() will automatically add it to the list!
class Newspaper extends LibraryProperty{}
//new Newspaper() will automatically add it to the list!

//Java effectively does:
// - create a new Book object
// - call the LibraryProperty constructor
// - inside that constructor, run all.add(this)
// - At that moment, this refers to the actual object being created, which is a Book.

16
New cards

Test Syntax

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class MyTestSuit{
	@Test public void testDogBarks(){
		Dog dog = new Dog(...);
		String sound = dog.bark();
		assertEquals("Woof", sound);
	}
}

var re= RuntimeException.class;
assertThrows(re, ()->new MyDate(0,0,0) );

Explore top flashcards

flashcards
Chemistryy
34
Updated 1208d ago
0.0(0)
flashcards
Social Studies Final Review
63
Updated 1043d ago
0.0(0)
flashcards
Chapter 24
69
Updated 1232d ago
0.0(0)
flashcards
US History Chapter 10 Test
32
Updated 106d ago
0.0(0)
flashcards
AP Lang Terms "B-H"
44
Updated 1200d ago
0.0(0)
flashcards
MAAN Quotes
22
Updated 1062d ago
0.0(0)
flashcards
Animal Quiz for Biology
29
Updated 1098d ago
0.0(0)
flashcards
Chemistryy
34
Updated 1208d ago
0.0(0)
flashcards
Social Studies Final Review
63
Updated 1043d ago
0.0(0)
flashcards
Chapter 24
69
Updated 1232d ago
0.0(0)
flashcards
US History Chapter 10 Test
32
Updated 106d ago
0.0(0)
flashcards
AP Lang Terms "B-H"
44
Updated 1200d ago
0.0(0)
flashcards
MAAN Quotes
22
Updated 1062d ago
0.0(0)
flashcards
Animal Quiz for Biology
29
Updated 1098d ago
0.0(0)