Created
October 1, 2019 17:27
-
-
Save lbattaglioli2000/83460334f74b2cd8c4a1e09c4c1e843e to your computer and use it in GitHub Desktop.
A test Gist to show you how a Gist works!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyGist { | |
public static void main(String[] args){ | |
System.out.println("Java can be fun, you just need a good teacher lol! I'm here to help!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here, you can write comments and leave code snippets like
System.out.println("This!");
. We can also add big blocks of code, rather than inline snippets. For instance, say we want to initialize a Scanner object to read user input. Here's the code we would use to do something like this:However, as far as the Java compiler is concerned right now, the
Scanner
class doesn't exist within our code yet, and will give us an error if we try to create an instance of it! We must import it! We can do so by simply adding the following line at the tipy top of our Java file (before ourpublic class
definition)!By adding this line, the Java compiler will look inside of the package
java/util
(which is basically just a folder as far as we're concerned) for a class calledScanner
. Let's pause for a moment, take a step back, and talk about classes and objects.Classes and Objects
In the real world, you often have many different objects of the same kind. For instance, think of your bicycle. It is just one of the many bicycles in the world. Using object-oriented jargon/terminology, we say that your bicycle is an instance (or an object) of the class of objects known as bicycles.
We have actually created an instance of a class already! When we say
new Scanner(System.in)
we are instantiating a newScanner
object (or creating a new instance of the classScanner
, in other words!)If we stick to the bicycle example, bikes have some state of being (whether it be the current gear, number of wheels, brand, etc.) as well as behavior. For instance, some bike-like behavior would be the ability to change gears, brake/slow down, ring your bell, etc. However, another key takeaway is that each bicycle's state is independent of and can be different from that of other bicycles. You could have a hot pink RadioFlyer, or you could have a carbon fiber mountain bike. They're both bikes, but their state of being is totally different from one another.
When building bicycles, manufacturers take advantage of the fact that bicycles share similar characteristics with one another and can build many bicycles from the same blueprint. It would be very inefficient to make a brand new blueprint for every individual bicycle manufactured. In object-oriented software, it's also possible to have many objects of the same kind that share characteristics. For instance, think of:
and so on and so forth. Like the bicycle manufacturers, you can take advantage of the fact that objects of the same kind are similar and you can create a blueprint for those objects. A software blueprint for objects is called a class.
Now let's get back to our regularly scheduled programming (ba dum tss!). Since we now know a little more about objects, we can continue to read our code. Let's actually grab a number from our user now! We could do something like the following, within our
main
method:Okay, let's talk about this.
Code Breakdown
The first line is pretty straightforward. We simply prompt the user to enter a number. That's really it.
The second line is a little more involved. Let's break it down. Firstly, the
int
flag tells the Java compiler that we're declaring a variable that is going to be anint
(integer, which is a positive or negative, whole number). Thisint
flag is important because Java uses it to tell the computer just how much space within memory it should set aside to hold onto this value throughout the lifecycle of our program. The next component of this line is the identifier. The identifier is basically just the name we give to this variable. In this case, the identifier isuserInputtedNumber
. We can now refer to the integer value of this variable by referring to it by its identifier. The=
sign means that we're assigning a value into the variable,userInputtedNumber
. The value that we're storing into this variableuserInputtedNumber
comes from the methodnextInt()
within ourScanner
object. A method is a bit of code that carries out a certain function. In this case, our methodnextInt()
simply reads from the keyboard, whatever number the user enters. It then returns that value, and stores it inside ofuserInputtedNumber
.Lastly, we use the
printf
method to display what number they entered into the command line. We use the%d
flag to tell the function that we want to plugin the value inside ofuserInputtedNumber
for the%d
flag. It would output something like: