Learn to Code via Tutorials on Repl.it!

← Back to all posts
Let's Learn Java I
AdCharity (1360)

Intro to Java


As the first of the upcoming series, I might as well preface why I'm starting Java tutorials. Frankly, I'm getting rather concerned about my life and coding career (mind you I'm not going to college until another 4 years), but when I found out that some programming languages have certain benefits, I figured I might as well learn them. I was rather estatic when I discovered Java, is the Computer Science AP test language. Easy money - learn java now and ace the AP test. While I'm at it, I'm going to share what I learned with you. I'll be pretending like you know NO CODING AT ALL, so this is kind of boring. But if you're interested in learning along with me, whatever floats your boat.


Don't blame me if you still can't code java - this stuff is confusing

Basic Stuff

Java was developed by a team at Sun Microsystems CA, most noteably a guy named James Gosling. While java has basic if-else statements and loops, the power of Java is found in objects.


Classes - has objects that interact with other classes
Packages - a bunch of classes, most of which are provided by the compiler
java.lang - contains most used classes; usually added to all Java programs


(obviously, replace anything in brackets with the appropriate class or package)
To import a class in a package, you must use:
import [package name].*;
To import a single class from a package, you must use:
import [package name].[class name];
Java also has subpackages, which you can access in a similar way.
import [package name].[subpackage name].[class name]
import allows the programmer to use objects and methods (or for JS people, functions) from other files written by yourself or other people.


All Java programs must have at least one class, what is usually called main method (very similar to Python).
All Java methods must be in a class.
All Java statements must be in a method.


The main method doesn't have more methods in it.
public, class, static, void, main, among others are reserved words (or key words) that cannot be renamed. If the compiler for some reason allows you to rename them, DO NOT - as reserved words, they have there own respective tasks.
static refers to methods/functions that will not access objects of a class - the main method is always static.

Applications vs Applet

Applications are basically java code, saved as program.java - applets are programs that run inside a web browser/applet viewers.


Java commenting is kind of confusing in a way (but sometimes not). Commenting usually looks something like this (note the / and /):

* Eat cheese method
* @param eat defines the type of cheese to be eaten
* @param per defines the person to eat the type of cheese
* @return rate of cheese eating
* @throws no more cheese (ok a real error will probably be here) if no person eats cheese.
//Also a comment

But what about the whole @param whatever thing? Basically @param, @return, and @throws are commenting terminology. As suggested by the name, it tells the user what parameters (stuff a function will take), the expected output, and any errors if something goes amiss.


identifier refers to a variable, parameter, method, class, or constant. It's made of letters, digits, and underscore characters, but cannot start with a digit. They are also case sensitive, meaning Hello and hello are NOT THE SAME. Usually, they are called what they are supposed to do - for instance, volume is probably better than say, akldsfjaklsdjf.
Identifiers are usually written in Camel Case (or underscores), which basically means you upper case words to distinguish them. For instance, checkCubeSides is readable because we have used Camel Case to essentially show solid words.
Identifiers cannot be called by any reserved words (see Conventions).


All identifiers have a type - which makes it somewhat more complicated than most languages. Built in types include int, boolean, double, and if you're feeling smart, char.
int - if you know math, its literally a positive or negative number that is not a fraction/contains decimals. Ints are kind of special in that they have a fixed amount of memory. If a value is too large in an int, you will likely get the wrong result WITHOUT warning.
boolean - true or false
double - a floating point number, basically something with a decimal like 2.239479823748923
Usually variables are declared by a type immediately. For instance, we can have

int countChars = 1;
double y,z;
boolean whyyyyy;

If that's somewhat confusing, it is. In JavaScript let and var covers pretty much everything. Here's something to chew on:

int num = 5;
double perciseNum = num;
//perciseNum is 5, but is a DOUBLE!!! It's kind of useful in certain situations it seems, and you don't have to explicitly state when you assign a double to an int value. HOWEVER...

double num = 6.8;
int secondNum = num;
//DOES NOT WORK! You have to explicitly say this yourself, such as:

double num = 6.8;
int secondNum = (int) num;
//This will work now. Note that secondNum will return 6, as it just removes the decimal part.

Now why is all of the stuff above (regarding defining doubles and ints) actually useful? If you wanted to round to the nearest integer, you could do something like this:

double integer = 5.123123123;
int roundInteger = (int) (integer + 1)
//Should return 6, no decimal.

Storing Numbers

Integers in Java are stored as string of bits - binary digits. Bits also store the sign of the integer (0 is postive and 1 is negative). int uses four bytes, or 32 bits. The largest possible inteer is (2^31) - 1.
Random stuff (if this gets confusing forget it) - there are built in Java types besides int. For instance, byte holds one byte, short holds 2 bytes, int holds 4 bytes, and long holds 8 bytes. Of course, no one really needs to know that right now.

Next up: https://repl.it/talk/learn/Lets-Learn-Java-II/25046 (thanks @Highwayman)

Highwayman (1458)

Awesome! I’m definitely going to follow

AdCharity (1360)

@Highwayman :) I'll try to release at least one every two days just as a self commitment, but sorry if it gets boring (had to swallow a bit when I read up on this)

Highwayman (1458)

@AdCharity lol. I’m not entirely worried about it getting boring, I’m kinda enjoying connecting attributes of c++ and java.

AdCharity (1360)

@Highwayman ok clearly I took a break its been longer than two days (I'm making one rn)

Highwayman (1458)

@AdCharity lol, I just assumed you were on the Christmas break. Awesome!

AdCharity (1360)

@Highwayman yeah plus I got frustrated with this:

Highwayman (1458)

@AdCharity Oh my. I don’t think I ever did neural networks actually, but you pain’s pretty easy to understand there lol.

JacobMacLeod (11)

@Highwayman Stupid question, but how do you follow it?

Highwayman (1458)

@JacobMacLeod there is a link at the end of the tutorial that goes to the next tutorial.

(It probably would have been better actually if AdCharity had made it a class, now that I think of it. Hm..)

AdCharity (1360)

@Highwayman I’m actually kind of dead right now I got more projects

Highwayman (1458)

@AdCharity So that’s why it’s been forever! Yeah no shade or pushing, it’s fine how it is, seeing as it’s a tutorial, not a class.

AdCharity (1360)

@Highwayman well not that I don’t want to do something like that, it’s more like I have no time :( you realize that life gets harder I guess

AdCharity (1360)

@Highwayman I was hella active last year but now I got to study (well I can release the third one when I get a reasonable repl with it; maybe sometime on Monday when my projects are due)

Highwayman (1458)

@AdCharity yeah I feel you, that’s basically me - cutting out the active part lol.
Monday? That’d be cool. :P

almostStatic (100)

Im more of a JS person myself.
Still gonna use PY for my Computer science coursework mainly because you can easily get inputs from console

AdCharity (1360)

@static2020 I agree (but also suck at python)

Andi_Chin (220)

maybe I'll learn how to print out "hello world" in 10 years. JK I love the series

JamesWanzer (1)

this is so confusing

AdCharity (1360)

@JamesWanzer :/ I assume you have some basic coding knowledge... even simple logic of any kind would work

AdriaDonohue (51)

i will try to learn as much as i can.this was very helpfull!



V3rmillionNet (54)

You forgot to say that java runs inclusive on your pencil case.

Im looking forward this! thanks!

theangryepicbanana (1691)

Why did you post this twice?

AdCharity (1360)

@theangryepicbanana I did?? sorry I was hotspotting myself... if I did I probably didn't even notice (I actually fell asleep sorry). I'll delete the other one.