Java 1

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

1/34

encourage image

There's no tags or description

Looks like no tags are added yet.

35 Terms

1
New cards

Why use Java

  • Works on multiple platforms

  • Most popular programing language in the world

  • Easy to learn


2
New cards

What is the main() Method

The main() method is required in every Java program. It is where the program starts running

3
New cards

Example of Java syntax

public class Main {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}


4
New cards

What is println() Method

The println() method to print a line of text to the screen

5
New cards

Example of how to write out println()

System.out.println("Hello World");


6
New cards

‘Create a porgam that outputs “Hello, Java”

public class Main {
	public static void main(String[] args) {
		System.out.println("Hello, Java");

	}

}


7
New cards

What is the print() method

Similar to println() but it does not insert a new line at the end of the output

8
New cards

What is the difference between the println() compared to the print()

The main difference is that println() does insert a new line at the end of the output whereas print() does not.

9
New cards

How do you print numbers in Java

System.out.println(3);
System.out.println(358);
System.out.println(50000);


10
New cards

True or False you can perform mathematical operations in println() method

True

example: System.out.println(3 + 3); → Output: 6

11
New cards

Create a program that to print your name and age

public class Main {
  public static void main(String[] args) {
    System.out.println("Philippe");
    System.out.println(20);
  }
}


12
New cards

Create a program that calculates 22 × 3

public class Main {
  public static void main(String[] args) {
    System.out.println(22 * 3);
  }
}


13
New cards

What is a single line comment

  • Single-line comments start with two forward slashes (//).

  • Usually used for short comments (single lines)


14
New cards

How do you write a singe line comment

//This is a comment
System.out.println("Hello World");


15
New cards

What is a multi line comment

  • Multi-line comments start with /* and ends with */

  • Usually used for larger comments (so like more of a couple of sentences)


16
New cards

How do you write a multi line comment

/* The code below will print the words Hello World
to the screen, and it is amazing */
System.out.println("Hello World");


17
New cards

What are variables

Variables are containers for storing data values.

18
New cards

List the different types of variables in Java

  • String → Stores text, such as “Hello”

  • int → Stores integers (so whole numbers, no deciamls), such as 123 or -123

  • float → Stores floating point numbers, with decimals, such as 19.99 or -19.99

  • char → Stores single characters, such as ‘a’ or ‘B’

  • boolean → Stores values with two states ,True or False


19
New cards

Declaring(/creating) variables in Java

  • Choose a type (like int or String)

  • Give the variable a name (like x, age, or name)

  • Optionally assign it a value using =


Example syntax:

type variableName = value;


20
New cards

Create a variable called name of type String and assign it the value "John".
Then use println() to print the name variable.

String name = "John";
System.out.println(name);


21
New cards

Create a variable called myNum of type int and assign it the value 15,

Then use println() to print the myNum variable.

int myNum = 15;
System.out.println(myNum);


22
New cards

int myNum = 15; Change the value of myNum from 15 to 20

int myNum = 15;
myNum = 20;  // myNum is now 20
System.out.println(myNum);


23
New cards

What are Final Variables

Its for when you don’t want to overwrite existing values.

(this will declare the variable as "final" or "constant", which means unchangeable and read-only)

24
New cards

Example of Final variable with error

final int myNum = 15;
myNum = 20;  // will generate an error: cannot assign a value to a final variable


25
New cards

Example of how to declare a String variable

String myText = "Hello";		


26
New cards

Example of how to declare an integer variable

int myNumber = "123";


27
New cards

Example of how to declare a float variable

float myFloat = 5.99f;


28
New cards

Example of how to declare a char varaible

char myChar = "a";	


29
New cards

Example of how to declare a boolean varaible

boolean myBool = true;


30
New cards

How to display variables in java


You can use + to combine things (so string concatenation, like in python )

String name = "John";
System.out.println("Hello " + name);


31
New cards

You can also + to add two varaibles together.

Example combine firstName and lastName to one variable fullName and print it

String firstName = "John ";
String lastName = "Doe";
String fullName = firstName + lastName;
System.out.println(fullName);


32
New cards

What are the two meanings for the + in Java

  • For text (so strings), it joins them together

  • For numbers, it adds values together


33
New cards

Add together the values x = 5 and y = 6 and print the results

int x = 5;
int y = 6;
System.out.println(x + y);


34
New cards

What would the output of this be:

int x = 5;
int y = 6;

System.out.println("The sum is " + x + y);


The output is:

The sum is 56

35
New cards

What would the output of this be:

int x = 5;
int y = 6;

System.out.println("The sum is " + (x + y));


The output is:

The sum is 11