[Go to site: main page, start]

0% found this document useful (0 votes)
7 views5 pages

Java Console Printing Basics

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Java Console Printing Basics

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

14/10/2024, 17:59 Tutorial: Java Documentation - Basics | CodeHS

 (/home)  Curriculum (/curriculum)  Tutorials (/tutorial/ )  Java Documentation - Basics

Java Tutorial

Java Documentation - Basics

By Zach Galant
Co-Founder of CodeHS

Printing to the console


Given a string str you can print to the console using [Link] or
[Link]

// Prints a line and ends with a new line


[Link](str);

// Prints a line but doesn't end the line. the next call to print or println will continue o
[Link](str);

Variables
You can create an integer variable with the keyword int like so

[Link] 1/5
14/10/2024, 17:59 Tutorial: Java Documentation - Basics | CodeHS

// Creates a variable with no value yet


int myVariable;

 (/home)  Curriculum (/curriculum)  Tutorials (/tutorial/ )  Java Documentation - Basics


// Creates a variable with a value
int myOtherVariable = 5;

Methods
Methods can take in values, called parameters.

The method below takes in a parameter called input and prints it.

private void printText(String input)


{
[Link](input);
}

Methods can also return a value.

The method below takes in a value, adds two to it, and returns it.

private int addTwo(int number)


{
return number + 2;
}

User Input
You can collect user input in programs with a Scanner

Initializing a Scanner

import [Link];
Scanner scanner = new Scanner([Link]);

[Link] 2/5
14/10/2024, 17:59 Tutorial: Java Documentation - Basics | CodeHS

Read a string

String str = [Link]();


 (/home)  Curriculum (/curriculum)  Tutorials (/tutorial/ )  Java Documentation - Basics

Read an integer

int num = [Link]();

Read a double

double myDouble = [Link]();

Read a boolean

boolean bool = [Link]();

Comparison Operators
Comparison operators return booleans (true/false values)

x == y // is x equal to y
x != y // is x not equal to y
x > y // is x greater than y
x >= y // is x greater than or equal to y
x < y // is x less than y
x <= y // is x less than or equal to y

Comparison operators in if statements

if (x == y)
{
[Link]("x and y are equal");
}

[Link] 3/5
14/10/2024, 17:59 Tutorial: Java Documentation - Basics | CodeHS

if (x > 5)
{
[Link]("x is greater than 5.");
 (/home)  Curriculum (/curriculum)  Tutorials (/tutorial/ )  Java Documentation - Basics
}

Math
Operators

+ // Addition
- // Subtraction
* // Multiplication
/ // Division
% // Modulus (Remainder)
() // Parentheses (For order of operations)

Examples

int z = x + y;
int w = x * y;

Increment (add one)

x++

Decrement (subtract one)

x--

Shortcuts

x = x + y; x += y;
x = x - y; x -= y;
x = x * y; x *= y;
x = x / y; x /= y;

RELATED TUTORIALS

[Link] 4/5
14/10/2024, 17:59 Tutorial: Java Documentation - Basics | CodeHS

 (/home)  Curriculum (/curriculum)  Tutorials (/tutorial/ )  Java Documentation - Basics 


([Link]

Products Use Cases Platform Curriculum (/curriculum)


Coding LMS (/lms) District (/info/districts) Assignments (/assignments) Course Catalog (/curriculum/catalog)
Online IDE (/ide) Schools (/schools) Classroom Management Project Catalog (/curriculum/projects)
CodeHS Pro (/pro) Teachers (/teachers) (/classroom_management) K-12 Pathways (/curriculum/pathways/k-
Computer Science Curriculum Grading (/grading) 12)
(/curriculum) Data (/progress_tracking) AP Courses (/curriculum/catalog?
Certifications (/info/certifications) Write Code (/explore/sandbox) tag=AP Courses)
Professional Development (/pd) Integrations (/integrations) Elementary (/curriculum/elementary)
AI Tools (/ai_tools) State Courses (/states)
Spanish Courses (/curriculum/spanish)
Standards (/info/standards)
Hour of Code (/hourofcode)
Practice (/practice)
Tutorials (/tutorials)
Digital Textbooks (/textbooks)
AP CSA Hub (/curriculum/ap-csa)
AI Curriculum (/ai)

PD (/pd) Programming Languages Resources Company


Online PD Courses (/pd/online)  New Sandbox Program Case Studies (/info/stories) About (/about)
In-Person PD Workshops Javascript States (/states) Team (/team)
(/info/pd/inperson) (/explore/sandbox/javascript) Testimonials (/testimonials) Careers (/careers)
Virtual PD Workshops (/info/pd/virtual) Python (/explore/sandbox/python) Tweets (/tweets) Privacy Center (/privacycenter)
Free PD Workshops (/pd/free) Java (/explore/sandbox/java) Read Write Code Blog Terms (/terms)
Teacher Certification Prep HTML (/explore/sandbox/html) ([Link] Privacy Policy (/privacy)
(/pd/certification_prep) C++ (/explore/sandbox/c++) Read Write Code Book Security (/security)
Microcredentials (/pd/microcredentials) SQL (/explore/sandbox/sql) ([Link] Accessibility (/accessibility)
PD Membership (/pd/membership) Karel (/explore/sandbox/karel) Knowledge Base
([Link]
Webinars (/webinars)
Student Projects (/projects)
Career Center (/careercenter)

 CodeHS Certified Educators (/info/certified_educators)

 CodeHS Educator Facebook Group ([Link]

 CodeHS Store ([Link]

[Link] 5/5

You might also like