- Introduction
- Super Simple Formula
- Your First Program
- Variables, Methods and Parameters
- Variables in Programming and Algebra
-
Math and Comparison Operators - If Statement
- While and For Loops
- Loopy Thinking
-
Data Types - Booleans
- Logical Operators (and Booleans)
- Output and Printing
- Printing and Loops
-
Arrays - Arrays and Loops
- Array Loop Practice
-
What's Next? - Quick Reference
Quick Reference
Quickly lookup Java basics, and load code examples directly into editor.
int
), the method name (doStuff
), and the method's parameters (such as a
and b
). The method body follows, where you usually need to write code that returns a value at the end.
//method header: methodName(parameters)
static int doStuff(int a, int b){
//your code here - erase this comment and write your code!
return 1;
}
Syntax note: Every line of code in Java needs to end with a
;
or it will be considered part of the next line. Comments are marked with 2 backslashes //
and are ignored by the computer.%
operator returns the remainder after division.
int product = 4 * (1+2) / 2; //6.
int roughDivision = 8 / 3; //2
int remainder = 3 % 2; //1, the remainder of 3/2
booleans are used to store true/false values, such as the results of comparisons:
boolean check = false; //this will store result of comparison
int five = 5;
//the following are all true
check = (five == 5); //equals
check = (five != 3); //not equals
check = (five >= 5); //greater or equals
The logical operator AND &&
will return true if multiple conditions are all true, and OR ||
will return true if at least one condition is true.
//these pointless statements will both set check to true
check = (3>2 && 2>1); //AND
check = (3>2 || 1>3); //OR
if(condition){
//doSomething
}
The else statement will execute code if the condition was false. The while statement will repeatedly execute the same code while a condition is true.
public static void controlFlow(int number) {
//this if/else block will print out 1 statement for any number
if(number > 10){
System.out.println("big number");
}
else if(number > 5){
System.out.println("medium number");
}
else{
System.out.println("small number");
}
//this loop will print the numbers from 0 to 9
int i = 0;
while(i < 10){
System.out.println(i);
i = i + 1;
}
}
System.out.println("");
The above prints text and add a newline afterwards.
System.out.print
prints without a newline, but you can add in spaces " " to separate text.
System.out.println("hello"); //prints with newline
String word1 = "hello";
String word2 = "world";
System.out.print(word1 + " " + word2); //prints "hello world"
Arrays are used to store multiple items of one data type together. They are declared with the type they will hold and with brackets []
. This code creates an empty 5-cell array:
int[] ar1 = new int[5];
This shortcut creates an array with numbers 0 to 4:
int[] nums = {0,1,2,3,4};
You can use for-loop to print values of array:
for(int i=0; i < nums.length; i=i+1){
System.out.print(nums[i]+" ");
}
Output and Printing
Until now we've just been returning data from our method, but we didn't discuss what how data can get out of a program for people to view. While regular programs for users normally have a visual interface (with things like buttons and menus), they are more complicated than is needed. The simplest way to get data from a program is to print it to the Standard Output. The Output is a place to display text to either the programmer or a command-line user. When learning programming, people usually just print to the Output instead of connecting the whole program to a visual interface.
This is the syntax to print the variable something
:
System.out.print(something);
To print something
on its own line you type:
System.out.println(something);
You can also print Strings directly, without making any variables:
System.out.println("hello world!");
Note that System
is capitalized, just like String was.
The I/O Code
All of the programming challenges required printing to the output, and if you look at the bottom of the provided code, you'll see this:
System.out.println(result);
result
was a variable with your solution, and to print it, you type System.out.println()
. Whatever is in-between the ( )
gets printed.
println
stands for "Print Line", which means it will add a new line after it prints, but if you didn't want that, you can just type System.out.print()
.
Printing only works for Strings and characters, but Java automatically converts numbers into Strings when you try printing them, which is why the provided code was able to print your number answers .
Printing Examples
Code | Output | Explanation |
---|---|---|
System.out.println("hello"); | hello | Simple print statement. |
System.out.println('a'); | a | Print a character. |
System.out.println("ab"+"cd")'; | abcd | Prints the 2 Strings combined. |
System.out.println(3); | 3 | Automatically converts a number to String for printing. |
System.out.println(32 + 1) | 33 | Calculates sum of numbers before printing them. |
System.out.println("32" + "1") | 321 | Combines the Strings (which happen to represent numbers) into one String for printing. |
String fruit = "apple"; System.out.println(fruit) | apple | Prints the value of the fruit variable. |
Challenge
You will be given a name
as input. Please print a message "Hello [name]!" on its own line. (Replace [name] with the given name). In this challenge, you need to write your own code to print the message.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Ian
Nov 8, 6:37 PMpublic class Main {
public static void doStuff(String name){
name = name + "!";
String hello = "Hello ";
String message = hello + name;
System.out.printIn(message);
}
How come each input doesn't print out on their own line?
Learneroo
Nov 9, 11:54 PM@Ian, Sorry, the correct code is with a lowercase 'L', not an uppercase I:
System.out.println
. It stands for Print Line.Ian
Nov 10, 5:42 PMThanks, this same issue had me stumbled for both of the Array Loop Practices too.
Cliff Karlsson
Feb 14, 1:53 PMYou can complete the task with only one line: System.out.printIn([Some Stuff]);
Dino Šišić
Feb 19, 2:31 PMI agree with Cliff, that is how I did it:
Hint:
string + variable + char
Minh
Feb 28, 5:00 AMnice article
Devin Cable
Mar 31, 9:29 PMIt's just as simple as forgetting the exclamation point to be incorrect.
Adhurim Esati
Sep 7, 8:53 AMI got all the results correct, and it says "Incorrect". Why ?
Adhurim Esati
Sep 7, 8:54 AMpublic static void doStuff(String name){
name = "Jim!";
String hi1 = "Hello ";
String hey1 = hi1 + name;
System.out.println(hey1);
name = "Sarah!";
Here is how I did it but still says Incorrect even though the Results are all Correct.
Adhurim Esati
Sep 7, 9:23 AMI don't quite understand this, can someone help me by explaining why
when I do
name = "Jim";
System.out.println("Hello " + name + '!');
it prints four
Hello Jim
Hello Jim
Hello Jim
Hello Jim
I don't understand.
Learneroo
Sep 7, 9:29 AM@AdhurimEsati, you are given the the String
name
each time as a parameter, you just need to print that.flowra
Feb 13, 5:03 PManother simple solution :
System.out.println ("Hello "+name+"!");
Asadujjaman Shimul
Dec 3, 3:08 AMimport java.util.Scanner;
public class Main {
public static void main (String[] args){
}
rajni dound
Nov 18, 4:22 PMimport java.util.Scanner;
public class Main(){
public static void main(String[] args){
String name="Nisha";
System.out.print("Hello" + name);
}
}