Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Tutorial_ Java Documentation - Basics _ CodeHS

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)
9 views

Tutorial_ Java Documentation - Basics _ CodeHS

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
You are on page 1/ 5

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 System.out.println or
System.out.print

// Prints a line and ends with a new line


System.out.println(str);

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

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

https://codehs.com/tutorial/zach/java-documentation-basics 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)


{
System.out.println(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 java.util.Scanner;
Scanner scanner = new Scanner(System.in);

https://codehs.com/tutorial/zach/java-documentation-basics 2/5
14/10/2024, 17:59 Tutorial: Java Documentation - Basics | CodeHS

Read a string

String str = scanner.nextLine();


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

Read an integer

int num = scanner.nextInt();

Read a double

double myDouble = scanner.nextDouble();

Read a boolean

boolean bool = scanner.nextBoolean();

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)
{
System.out.println("x and y are equal");
}

https://codehs.com/tutorial/zach/java-documentation-basics 3/5
14/10/2024, 17:59 Tutorial: Java Documentation - Basics | CodeHS

if (x > 5)
{
System.out.println("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

https://codehs.com/tutorial/zach/java-documentation-basics 4/5
14/10/2024, 17:59 Tutorial: Java Documentation - Basics | CodeHS

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


(https://www.facebook.com/codehs)(https://www.instagram.com/codehs/)(https://www.linkedin.com/company/codehs)(/youtube)(https://twitter.com/codehs)(https://www.threads.ne

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) (https://readwritecode.blog) Privacy Policy (/privacy)
(/pd/certification_prep) C++ (/explore/sandbox/c++) Read Write Code Book Security (/security)
Microcredentials (/pd/microcredentials) SQL (/explore/sandbox/sql) (https://geni.us/ReadWriteCode) Accessibility (/accessibility)
PD Membership (/pd/membership) Karel (/explore/sandbox/karel) Knowledge Base
(https://help.codehs.com)
Webinars (/webinars)
Student Projects (/projects)
Career Center (/careercenter)

 CodeHS Certified Educators (/info/certified_educators)

 CodeHS Educator Facebook Group (https://www.facebook.com/groups/codehseducators)

 CodeHS Store (https://store.codehs.com/)

https://codehs.com/tutorial/zach/java-documentation-basics 5/5

You might also like