Java Basics and Data Types

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

1/53

encourage image

There's no tags or description

Looks like no tags are added yet.

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

No analytics yet

Send a link to your students to track their progress

54 Terms

1
New cards

How does System.out.print and System.out.println differ?

Both send text to standard output.

  • println appends a newline at the end.

  • print does not.

2
New cards

Newline escape sequence

\n

3
New cards

Backspace escape sequence

\b

4
New cards

Tab escape sequence

\t

5
New cards

Carriage return escape sequence

\r

6
New cards

Backslash escape sequence

\\

7
New cards

Single quote escape sequence

\'

8
New cards

Double quote escape sequence

\"

9
New cards

How do you write the main method starting point?

public static void main(String[] args) {
    // code here!
}
  • public accessible from anywhere.

  • static belongs to the class, not an object.

  • void does not return any value.

  • main the name of the method.

  • String[] args parameter for command-line arguments.

10
New cards

Single line comment

// Everything on this line is ignored.

11
New cards

Multi-line comment

/* Everything in-between 
   is ignored */

12
New cards

Javadoc comment

/** Generates documentation, prints to screen */

13
New cards

Variable

A named memory location with a declared type.

  • Must be declared before use.

  • Must be initialised before reading.

14
New cards

What is the difference between variable declaration and assignment?

  • Declaration tells Java the name and type of the variable.

  • Assignment gives the variable a value.

int value;  // declaration
value = 5;  // assignment

int value = 5;  // both in one-line

15
New cards

Literal

A fixed value written directly in code.

16
New cards

Static typing

  • The type of every variable must be declared on creation.

  • Once declared, you can not assign a different type to the variable.

17
New cards

Why is static typing used?

  • Promotes better performance.

  • Makes code more robust.

18
New cards

Identifiers cannot start with a digit or use reserved keywords.

True

19
New cards

Identifiers can use letters, digits, _, and $.

True

20
New cards

Identifiers are case-sensitive and cannot contain spaces.

True

21
New cards

What naming convention do variables and methods use?

lowerCamelCase

22
New cards

What naming convention do classes use?

UpperCamelCase

23
New cards

What naming convention do constants use?

UPPER_SNAKE_CASE

24
New cards

How are constants declared?

Using the final keyword, for example:

final int DAYS_IN_YEAR = 365;

25
New cards

Constants can change value once initialised.

False

26
New cards

What are Java’s eight primitive types?

  • byte, short, long

  • int, float, double, char, boolean

27
New cards

Primitive types store raw values and are fast.

True

28
New cards

How do integer and floating-point types differ?

  • Integer types (byte, short, int, long) store whole numbers.

  • Floating types (float, double) store fractional values.

29
New cards

What is the default type for floating-point literals?

They are double by default, but we can append F or f to make it a float.

30
New cards

Literals cannot include commas or currency symbols.

True

31
New cards

What type of quotes do char values and Strings use?

  • char literals use single quotes. 'A'

  • Strings use double quotes. "A"

32
New cards

What is a reference type?

Reference types (arrays, Strings, enums) store the addresses of objects.

  • Uninitialised references default to null.

  • Methods are invoked on objects via references.

  • Assigning one reference to another makes both refer to the same object.

33
New cards

Primitive types in Java are predefined and built into the language, while reference types are created by the programmer (except for String).

True

34
New cards

Reference types can be used to call methods to perform certain operations, whereas primitive types cannot.

True

35
New cards

Primitive types start with a lowercase letter (like int), while non-primitive types typically starts with an uppercase letter (like String)

True

36
New cards

Primitive types always hold a value, whereas non-primitive types can be null.

True

37
New cards

What is an enum?

An enum defines a fixed set of constants, for example:

enum Level = { LOW, MEDIUM, HIGH };
Level x = Level.MEDIUM;

38
New cards

String literals may be interned in the String Pool, meaning?

Identical literals share the same reference.

39
New cards

How do we compare equality between Strings?

  • Compare content: strA.equals(strB)

  • Compare references: strA == strB

40
New cards

Strings are immutable, so their values can not be changed after creation.

True

41
New cards

String methods return new String instances.

True

42
New cards

How to align text with String formatting?

String word = new String("hi");
System.out.printf("%s...%n", word);  // no padding
System.out.printf("%10s...%n", word);  // right-align
System.out.printf("%-10s...%n", word);  // left-align
  • Second statement right-aligns word in a field of 10 characters.

  • Third statement left-aligns word in a field of 10 characters.

43
New cards

How to pad numbers with leading zeroes?

int id = 42;
System.out.printf("ID: %05d%n", id);
  • Print 42 as a 5-digit number, padded with leading zeroes.

44
New cards

Dividing two integers with / produces an integer result.

True

45
New cards

Implicit Conversion (Widening)

When a smaller type is assigned to a larger type. No data is lost.

byte > short > int > long > float > double

For example:

int a = 10;
double b = a;  // int converts to double automatically

46
New cards

Explicit Conversion (Narrowing)

Must be done manually using a cast. Can result in information lost.

For example:

double x = 9.8;
int y = (int) x;  // y = 9, decimal part is dropped

47
New cards

How to convert a boolean to another type?

This is not possible, you can not convert to a boolean or from any other type.

48
New cards

We can perform String to number conversion using?

String input = "123";
int number = Integer.parseInt(input);

String piStr = "3.14";
double pi = Double.parseDouble(piStr);

49
New cards

We can perform number to String conversion using?

int value = 42;
String text = String.valueOf(value);

50
New cards

How to read input in Java?

import java.util.Scanner;
Scanner = input = new Scanner(System.in);
System.out.print("Some kind of prompt message: ");
int num = input.nextInt();  // Some method to read input.

51
New cards

What Scanner methods exist to read different types of input?

  • nextInt() reads an integer.

  • nextDouble() reads a floating-point number.

  • next() reads a single word (String).

  • nextLine() reads an entire line of text.

52
New cards

String Pool

A special memory region where string literals are stored. When a new string is created using a literal, Java checks if the string already exists in the pool:

  • If it exists, return the reference to the existing string.

  • Otherwise, create a new string in the pool.

53
New cards

String methods

str.charAt(3);  // return character at index 3
str.substring(2,5);  // substring from index 2 to 4
str.length();  // get length of string
str.indexOf('x');  // return index of first occurrence
str.split("\\s");  // splits on whitespace
str.split(",");  // splits string on comma
str.concat(str);  // concatenates two strings
str.trim();  // removes whitespace from both ends

54
New cards

For mutable String operations, use?

StringBuffer Methods

  • Includes append and reverse operations.

Explore top notes

note
4.2: solutions and dilutions
Updated 1261d ago
0.0(0)
note
CGO casus 6
Updated 442d ago
0.0(0)
note
electricity
Updated 392d ago
0.0(0)
note
APWH Unit 1
Updated 696d ago
0.0(0)
note
Thermochemie
Updated 499d ago
0.0(0)
note
4.2: solutions and dilutions
Updated 1261d ago
0.0(0)
note
CGO casus 6
Updated 442d ago
0.0(0)
note
electricity
Updated 392d ago
0.0(0)
note
APWH Unit 1
Updated 696d ago
0.0(0)
note
Thermochemie
Updated 499d ago
0.0(0)

Explore top flashcards

flashcards
Sociedades Mercantiles
101
Updated 690d ago
0.0(0)
flashcards
bio practical 3
82
Updated 1098d ago
0.0(0)
flashcards
Spanish 2: La Salud
49
Updated 860d ago
0.0(0)
flashcards
Quiz 1
76
Updated 566d ago
0.0(0)
flashcards
Spanish Reflexive Verbs
24
Updated 1056d ago
0.0(0)
flashcards
Part 1 Vocab
32
Updated 179d ago
0.0(0)
flashcards
14. 抗議する義務
67
Updated 1212d ago
0.0(0)
flashcards
Sociedades Mercantiles
101
Updated 690d ago
0.0(0)
flashcards
bio practical 3
82
Updated 1098d ago
0.0(0)
flashcards
Spanish 2: La Salud
49
Updated 860d ago
0.0(0)
flashcards
Quiz 1
76
Updated 566d ago
0.0(0)
flashcards
Spanish Reflexive Verbs
24
Updated 1056d ago
0.0(0)
flashcards
Part 1 Vocab
32
Updated 179d ago
0.0(0)
flashcards
14. 抗議する義務
67
Updated 1212d ago
0.0(0)