It’s Michael, and today’s lesson will be a little different. There won’t be actual coding involved here, rather, I will discuss a topic which is important in Java-numbering systems.
What’s the point of numbering systems? Think of this way-we communicate with each other with words and numbers, but since computers only understand numbers, numbers are how computers communicate with each other. When we enter data in our programs, that data is converted into electronic pulse, which is then converted into numeric format by ASCII (an acronym for American Standard Code for Information Interchange), which is the system used for encoding characters (whether 0-9, A-Z lowercase or uppercase, or some special characters like the backslash and the semicolon) based off the four main numbering systems (binary, octal, hexadecimal, and decimal). Each character, of which there are 128, has their own unique ASCII value for each of the four numbering systems.
Now, let’s explain each of these four numbering systems:
Binary is a base-2 number system, as there are only two possible values-0 and 1. 0 represents absence of electronic pulse while 1 represents presence of electronic pulse. A group of four bits, like 1100, is called a nibble while a group of eight bits, like 01010011, is called a byte. The position of each digit in a group of bits represents a certain power of 2.
To find out the decimal form of 01010011, multiply each 0 and 1 by a power of 2. You can determine which power of two to use based on the position of the 0 or 1 in the group. For instance, the 1 at the end of the group would be multiplied by 2^0, since you would always start with the power of 0. Then work your way left until you reach the last bit in the group (and increase the exponent by 1 each time).
The number in decimal form is 83, which can be found through this expression (2^0+2^1+2^4+2^6). Granted, you don’t need to worry about the 0 bits, since 0 times anything is 0. And for the 1 bits, you just need to calculate the powers of two and add them together; don’t worry about multiplying 1 to each power since 1 times anything equals itself.
And remember to ALWAYS start with the power of 0 and work your way left, increasing the exponent by 1 each time.
Octal is very similar to binary, except it is a base-8 system, so there are 8 possible values-0 to 7 (Java tends to like to start with 0).
Octal-to-decimal conversions are very much like binary-to-decimal conversions, except you’re dealing with successive powers of 8 as opposed to powers of 2.
Take the number 145. What would this be in decimal form?
If you said 101, you’re right. You can find this out using the expression (1*8^2+4*8^1+5*8^0). Remember to multiply each digit by their respective power of 8, then find the sum of all the products.
And ALWAYS remember to start with the power of 0.
Decimal is the number system we use every day when talking about numbers; it is a base-10 system, so there are ten possible values (0 to 9). You are probably very familiar with this system, so there’s not much explaining to do here.
Hexadecimal is an interesting system for a few reasons. First of all, it is a base-16 system, so there are 16 possible values. Secondly, this is the only alphanumeric system of the four I discussed, as this system uses the digits 0-9 and the letters A-F which represent the numbers 10-15.
Hexadecimal-to-decimal conversions follow the same process as octal-to-decimal and binary-to-decimal conversions, except you’re dealing with successive powers of 16.
What do you think 45ACB could be in decimal form?
If you answered 285,387, you’re right. Here’s the expression: (4*16^4+5*16^3+A*16^2+C*16^1+B*16^0). Remember that A is 10, B is 11, C is 12, D is 13, E is 14, and F is 15.
Remember that these four systems aren’t the only possible number systems, as there are other number systems for several different bases. For instance, a base-4 system would use the numbers 0-3 and utilize successive powers of 4 in conversions. Here’s some things to know about other number systems:
Base-X means that there are X possible values (so base-9 would have 9 possible values)
The starting point for number systems is always ZERO; the endpoint is X-1 (so 5 would be the endpoint for a base-6 system)
You would convert base-X numbers to decimal form utilizing successive powers of X (so trying to convert a number in base-5 decimal form would utilize successive powers of 5-5^0, 5^1, and so on), multiplying each digit by the power of X, and finding the sum of all the products.
When the base is greater than 10, use letters of the alphabet for values above 9. So for base-19, use the letters A-I for the values 10-18.
Now, here are some conversion problems for you. Can you solve them? I will post the answers on the next post:
It’s Michael, and today’s post will be on if-else statements and logical/comparison operators in Java. If-else statements allow your program to choose between two or more alternative actions.
Why are if-else statements so important? Well, with Java programs, as in everyday life, things can go several different ways. Let’s say you just took a final exam for a college class. Depending on how you did on that exam, your grade can come out one of five ways (A, B, C, D, or F). The same sort of thinking applies to programming, as there can be several different actions a program can perform depending on certain scenarios (like if an int is greater than/less than/equal to a certain amount).
Let’s demonstrate using a program example (I had to copy and paste the code since you can’t see all of it from a screenshot):
package javalessons;
import java.util.Scanner;
public class JavaLessons
{
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
System.out.println (“Please enter a number: “);
double temperature = sc.nextDouble ();
System.out.println (“C or F: “);
String unit = sc.next();
double temp;
if (“C”.equals(unit))
{
temp = (1.8*temperature)+32;
System.out.println (“The temperature is ” +temp+ ” degrees Farenheit”);
}
else if (“F”.equals(unit))
{
temp = (temperature-32)*0.56;
System.out.println (“The temperature is ” +temp+ ” degrees Celsius”);
}
else
System.out.println(“Not a valid answer”);
}
}
This program is a temperature unit converter; the user enters a number and a “C” or “F” (for Celsius or Fahrenheit) and, depending on what letter was entered, converts the temperature from Celsius to Fahrenheit or vice versa.
I want to clarify that there is a difference between else if and else. Else if acts as another if statement; you would use else if if your program was trying to test two or more conditions, such as whether a “C” or “F” was typed. The else statement would come at the very end of your code since that would be the statement that executes should neither of the if/else if conditions be true; the output I used for the else statement would only display if neither a “C” nor an “F” was typed.
Always use .equals for String variables such as these. Don’t use =.
You need to use brackets if you if or else if statements use more than one line of code. If you only need one line of code, you don’t need the brackets.
Let’s try inputting a number:
run:
Please enter a number:
19
C or F:
C
The temperature is 66.2 degrees Farenheit
BUILD SUCCESSFUL (total time: 17 seconds)
I entered 19 and C, meaning I wanted to find out what 19 degrees Celsius was in Fahrenheit. I got 66.2, which means that 19 degrees Celsius is equal to 66.2 degrees Fahrenheit.
Now let’s see what happens when I don’t type a “C” or an “F”:
run:
Please enter a number:
45
C or F:
W
Not a valid answer
BUILD SUCCESSFUL (total time: 8 seconds)
I typed 45 and W and as a result, “Not a valid answer” displayed (which is the output I had set to display if neither “C” nor “F” was typed).
Now let me introduce some new concepts that perfectly relate to if and else if statements-comparison and logical operators.
Comparison operators are pretty self-explanatory, as they are used to compare two things (usually testing a variable and a value). If you know basic algebra, you’d be familiar with most of these:
== is “equal to” (remember to use .equals for Strings)
!= is “not equal to”
> is “greater than”
>= is “greater than or equal to”
< is “less than”
<= is “less than or equal to”
Logical operators are similar to comparison operators since both compare statements. However, comparison operators are only used for single boolean statements, such as (temperature > 70) while logical operators are used for groups of boolean statements, such as (temperature >= 60) && (temperature <= 80).
Here are the three main logical operators:
&&-AND
||-OR
!-NOT
OK, so even though I said logical operators are used for groups of boolean statements, the NOT operator is more for single boolean statements, such as !(temperature > 70)-meaning that you’re looking for temperatures that aren’t greater than 70 degrees. You can, however, also use the NOT operator for groups of boolean statements, such as (!(temperature >= 50) && (temperature <= 0))-meaning that you’re looking for temperatures that aren’t between 0 and 50 degrees.
Here’s the one takeaway with the NOT operator-it negates boolean statements (whether an individual statement or groups of statements). In other words, it essentially contradicts whatever is in the statement(s).
Now, one more thing I want to cover regarding logical operators-the truth table. The truth table doesn’t involve any coding, rather it’s a helpful guide with regards to boolean statements in your program. The truth table compares two boolean statements, looks at the values of each statement (whether they are true or false), and based on the values of each individual statement, returns true or false for that pair of statements. Here’s the table below:
When dealing with AND, both statements in a pair have to be to true in order for the statement pair to be true. If one or both statements are false, then the statement pair returns false. With OR however, only one statement has to be true for the pair to be true, but if both statements are false, then the pair is false. And as I mentioned earlier, NOT contradicts boolean statement(s), so if you not right by a boolean statement pair that is true, then that pair is false (and vice versa).
Let’s test this logic with an example program:
In this program I have two boolean statements (5 > 7) and (12 < 16). I then use System.out.println to test whether the statement pair greaterThan && lessThan is true or false. Since I used AND and one of the statements is false-greaterThan-both of the statements are false. Remember with AND, if one of the statements are false, then both are false.
Now let’s apply our knowledge of truth tables to more than two boolean statements:
When dealing with more than two boolean statements, logical evaluation is done from left to right. The Java compiler would first read the first pair of statements-greaterThan && lessThan-to evaluate whether that statement pair is true or false (it’s false). The compiler then takes the false result and compares it with the subtraction statement, which is true (thus statements 1-3 are collectively true). Finally, the compiler takes the collective true result from statements 1-3 and compares it with the multiplication statement, which is false. But, since OR is being used to compare the statements, you only need 1 true statement to result in true, which is the case here. After analyzing all 4 statements, the compiler states that the 4 statements together are collectively true.
Now, let’s analyze a program with several if options. In this program, you would type in a year, and the program will tell you what generation you belong to; keep in mind that the dates provided are just approximations, as different sources use different dates to mark the beginning and end of a generation. Here is the code:
package javalessons;
import java.util.Scanner;
public class JavaLessons
{
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
System.out.println (“Enter your birth year: “);
int year = sc.nextInt();
if (year >= 1900 && year <= 1928)
{
System.out.println(“You are part of the Greatest Generation”);
}
else if (year >= 1929 && year <= 1945)
{
System.out.println(“You are part of the Silent Generation”);
}
else if (year >= 1946 && year <= 1965)
{
System.out.println(“You are a Baby Boomer”);
}
else if (year >= 1966 && year <= 1981)
{
System.out.println(“You are a Gen-Xer”);
}
else if (year >= 1982 && year <= 2000)
{
System.out.println(“You are a Millenial”);
}
else if (year >= 2001 && year <= 2019)
{
System.out.println(“You are a Gen-Zer”);
}
else
{
System.out.println(“Invalid input”);
}
}
}
Please note that you will get a certain output (i.e. “You are a Millennial”) if both conditions are true; “You are a Millennial” will only print out if the year you typed is between 1982 and 2000.
Now let’s use the year 1946 (President Trump’s birth year):
run:
Enter your birth year:
1946
You are a Baby Boomer
BUILD SUCCESSFUL (total time: 3 seconds)
As you can see, 1946 corresponds to Baby Boomer, meaning President Trump is a Baby Boomer.
Now let’s try 1996 (my birth year):
run:
Enter your birth year:
1996
You are a Millenial
BUILD SUCCESSFUL (total time: 3 seconds)
According to the program, I am a millennial.
For those who want a video demo of the program, check this out-Generation program.
Now, a concept you should know that ties in with the material in this post is precedence. Precedence is similar to order of operations in the sense that both concepts involve doing things in a certain order. However, while order of operations just deals with arithmetic and exponents, precedence deals with arithmetic, comparison, and logical operators. With precedence, there is a certain order which the compiler evaluates statements with multiple arithmetic/comparison/logical operators. Here’s a handy guide:
First-the operators +,-,++,–, and !
The ++ and — operators represent incrementing and decrementing, respectively. These operators basically increase or decrease (or increment and decrement) a number by 1; you’ll see them more when I cover loops.
Second-the operators *, / and %
Third-the operators + and –
The plus and minus signs that take highest precedence actually refer to positive and negative numbers, while the one that are 3rd in precedence order actually refer to addition and subtraction
Fourth-the operators <, >, <= and >=
Fifth-the operators == and !=
Sixth-the operator &
Seventh-the operator |
Eighth- the operator &&
Ninth-the operator ||
The difference between the single & and || and the double && and || is that the former two are known as bitwise operators, which analyze data of types int, long, short, char, and byte bit by bit.
Check out this forum to learn more about bitwise operators-Bitwise Operators.
Keep in mind that parentheses ALWAYS take highest precedence (yes even higher than the operators listed as highest precedence). Any statements inside the parentheses are evaluated in order of operator precedence; if you’re dealing with parentheses-within-parentheses, the expression in the innermost set of parentheses is evaluated first, after which the compiler will work its way outward.
Here’s a series of statements; can you guess the result (true or false) based on precedence:
temperature = 44;
temperature – 2 < 32 || (temperature + 5 != 55) && temperature / 2 < 30
If you said true, you are right. Let me break it down:
The statement temperature + 5 != 55 is evaluated first, as it is in parentheses. It is true.
The next statement to be evaluated is temperature / 2 < 30, as it right by the && sign, which takes higher precedence than ||. That statement is also true, so that results, along with the true statement in parentheses, makes for a true statement pair.
The last statement to be evaluated is temperature - 2 < 32, since it’s right by the || sign. Even though this statement is false, the true statement pair to its right makes the whole group of statements collectively true (remember with OR, only one true makes for a collective true).
It’s Michael, and today’s post will be on user input, importing classes, and doing math in Java. I know these may seem like three totally unrelated topics, but I think they relate well to one another, which is why I’m going to cover all of them in this post.
First off, let’s discuss how to manage user input in your Java program. If you weren’t already aware, Java output doesn’t always have to be predefined-you can write your code so that you can choose the output.
The first thing you would need to do is import the Scanner class from Java, which is the class that handles all text input (don’t think about using it for graphics-heavy programs). Importing the Scanner class (or any class for that matter) allows you to use all its methods.
Here’s an example of a Scanner program:
The first thing I did was to import the Scanner class. I then created s, which is a variable of type Scanner. Remember to always create a Scanner variable every time you want to use the Scanner class in your program! And remember to ALWAYS set the parameters as (System.in).
I then used System.out.println to write the command asking the user for their input. The user’s input then is stored in a variable, in this case int year. You must always store your input in a variable if you want it to display on the screen. What variable type you choose depends on what kind of input you are seeking. In this case, since I am seeking integers, I want to use type int. The value you assign to the variable would be (name of Scanner variable).next(type of variable your input is). Since my Scanner variable is named s and my input variable is of type int, the value I assigned my variable was s.nextInt(). The next______ method you use depends on the input variable’s type, so if I had a char variable I would use s.nextChar(). Please note that there is no nextString method so for String variables use next or nextLine.
For those who would like a video demonstration of the Scanner class, check out this program-Scanner demo.
Now, Scanner isn’t the only class you can import from Java. In fact there are plenty of other classes (and methods) you can use and import for a variety of purposes, such as the class Calendar for calendar programs. The best part is that you don’t need to memorize all of the classes available on Java, as there is a handy online dictionary where you can look up classes and methods that you need for your program. This dictionary does a very good job of explaining how you can use each available class.
Now at the beginning of this post, I said I was going to cover how to do math in Java. Well, there is a certain class that I will show you how to use to do just that-it’s the Math class. To access this class, use the line import java.lang.Math. Here are three programs utilizing the Math class and its methods.
This program asks the user for a negative number, then finds the absolute value of that number. Since I typed -33, the program calculated 33 as the absolute value.
This program asks the user for two numbers, then will calculate the value of the first number raised to the power of the second number. Since my first number was 9 and my second number was 3, the program printed out 729, or 9 cubed (which is a more colloquial way of saying “to the third power”)
This last program asks the user for a number, which in this context is an angle measurement for a right triangle. The program will then display the cosine (recall SOHCAHTOA) for that angle measurement; since I used a 49 degree angle in this example, the cosine is 0.3006 (rounded to four decimal places).
However, one thing to note is that the Math is more geared towards complex mathematical operations (like finding logs, trigonometric ratios, and square roots). You don’t need the Math class for basic arithmetic.
In case you’re wondering, here are two programs showing how to do basic arithmetic in Java:
This program demonstrates simple division; the user is asked for a number divisible by 3, then the program will print out the quotient of that number divided by 3. Since I used 612, the quotient displayed was 204.
This program utilizes the order of operations (think PEMDAS); the user is asked to type in a number and the program will print out the answer to the problem (pemdas/2)*3+8-4. Remember pemdas is the name of the variable where you will store the number you choose. I chose to store the user input as a double because I wanted a precise answer; had I stored the input as int , I would’ve gotten the nearest whole number (12) instead of the more precise answer I wanted-11.5.
Something I should have pointed out earlier-ALWAYS remember to put semicolons after each statement! To be clear, this only means the lines of code inside each method or class along with any import statements. This doesn’t mean semicolons are needed after class/method declarations themselves!
This isn’t totally necessary when coding, but the // symbol right before a line of code is a way to take notes within your program. The compiler won’t read any line of code that begins with //.
Now before I go, here’s a video demonstration on doing math in Java (both with the Math class and without)-Java Math.
It’s Michael here, and today’s post will be on variables and data types in Java. Variables are pieces of memory in Java that store information necessary for your program to run. Take a look at this statement:
int gamesWon = 2
This assignment statement (which is what you call the line of code where you create a variable and give it a value) displays the variable’s name (known in Java jargon as an identifier), value, and data type (int). Data types, such as int, indicate what kind of value the variable is. In the above example, the data type int indicates that the value is an integer.
When naming your variables, remember that you can only use letters, the numbers 0-9, and the underscore character. Also, the first character of your variable name can’t be a number.
The variable names 4twenty, go-Browns, and Linkin....Park won’t work, and the complier will complain if you try to use any of them.
One thing I didn’t mention in the previous Java post is that Java is case-sensitive, so that variables like southPark, Southpark, and SouthPark would be treated as 3 different variables rather than the same variable. Having 3 same-named but differently-capitalized variables is perfectly acceptable in Java, though it will get confusing for you the programmer.
A peculiar Java syntax convention indicates that, with multi-word variable names like gamesWon, the first word (in this case games) should start with a lowercase letter and any subsequent words should start with an uppercase letter. It’s just good practice though; after all, the Java compiler won’t display error messages if you use GamesWon or gamesWon instead of gamesWon.
There are two types of variables in Java-primitive types and class types. Class type variables belong to a certain class (which can either be a built-in class such as String or Array or a class you create in your program). On the other hand, primitive type variables don’t belong to a certain class but are rather the eight basic types of variables in Java, which consist of:
byte
short
int
long
float
double
char
boolean
The first four variable types mentioned (byte, short, int, long) are all of type int. The only difference between them is the integer ranges they represent. Byte has the smallest range, spanning from only -128 to 127, while short goes from approximately -32,000 to 32,000, int from roughly negative 2 billion to positive 2 billion, and long from negative 9 quintillion to positive 9 quintillion.
Float and double are floating-point numbers, which is a more technical way of saying that float and double are decimals. Numbers like 3.5, -9.99, and 1.042 would qualify as either float or double. Like the four integer variable types mentioned in the previous paragraph, the only difference between float and double are the ranges they represent, with double having the wider range of decimals.
The last two variable types, char and boolean, don’t store numbers. Rather, char stores single characters, which can consist of any of the letters of the alphabet, the numbers 0-9, and symbols like the pound sign (or hashtag for the millennial crowd) or percent sign. Char can only be 1 character, so the variables g^ksf and dipppk can’t be of type char since they have multiple characters (String would be a more appropriate type). And yes, even though I said char doesn’t store numbers, a variable like char = 0 wouldn’t be regarded as a number. Boolean stores the values true and false and is used when dealing with logic statements (more on those in a future post). For instance the statement (41 > 55) would qualify as a logic statement by Java and thus would be boolean. If you’re wondering, this statement would return false (which can be figured out easily because 41 is less than 55).
Now, if you’d like to see how variables work, here’s a little demonstration:
Inside the main method, I declared an int variable-currentYear-and gave it a value-2019 (well, because that’s the current year). Remember to put the assignment statement inside the main method, because the compiler will complain if you try to do otherwise. I then asked the program to print out the currentYear, which it did.
One thing to know with System.out.println and variable names is that you can simply put the name of your variable in the parentheses and the value will print out.
For those that would like to see a video demonstration of how variables work, click this link-Variables Demo Java.
This is Michael, and first of all, Happy New Year! Can’t believe it’s already 2019! I hope you all had a wonderful holiday season. I also wanted to say thank you for reading my posts in 2018, and I hope you learned some new skills along the way. Get ready for some exciting programming and analytical content in 2019!
Now, since is the first post of 2019, I thought I’d try discussing something new-Java. Some of you might be wondering “What is Java?” Java is basically a general-purpose computing language that is used for Android apps (whether on the phone or tablet), server apps for financial services companies (think large hedge funds like Goldman Sachs), software tools (like NetBeans-more on that later), and games like Minecraft.
One thing to keep in mind with Java is that is more for building applications and programs. Java isn’t really useful for statistical analyses like R is, or database building/querying like MySQL.
Now to understand Java, I will discuss a concept called IDEs, or Integrated Development Environments. IDEs are basically the software systems you will use to write, test, and debug your programs. My personal favorite IDE is NetBeans, which is what I will use for all of my Java posts (there are other IDEs to choose from like JCreator, which I’ve used but didn’t like as much as NetBeans). NetBeans, and most other Java IDEs, are free to download; another perk is that they will point out and explain errors in your code (whether syntax or otherwise) by displaying a red line underneath the line(s) of code with the error(s). You might also see yellow lines underneath certain portions of code, but that is usually displayed to suggest how to make your code neater rather than to point out errors in your code.
So let’s get started and open up NetBeans:
This screen is what you will see every time you open up NetBeans. The other tabs contain other Java projects I’ve done in my spare time.
How do we create a new Java project? Here’s a video showing how to create a new Java project-Java Lesson 1.
As you can see, you must first create a project, then create a class. You can have several classes in your project, but for now let’s stick with a single main class.
Once you finish creating your project and class, you should see your interface, which looks like this:
The interface is where the magic of Java programming happens; here, you write, run, and debug the code you will use for your program.
Now let’s demonstrate the programming process using the most famous example for beginner coders-the “Hello World” program. Here’s a video demonstration for anyone interested-Hello World demo.
Some of you are probably wondering “What does all this code mean?” Let me explain.
A package is a mechanism for grouping classes. The package javalessons gets its name from our main class-JavaLessons. If we had several packages in our project, then they would all belong to the javalessons package.
There are two types of packages-user defined (like the javalessons package) and built-in (which come with Java). Some built-in packages include:
java.util– contains utility classes which implement data structures such as Dictionary; commonly used for Date/Time operations
java.awt– commonly used for graphical interfaces purposes (such as creating buttons or drop-down menus)
Two concepts that you should know are objects and classes, which are the two most fundamental concepts in Java.
Java is an object-oriented programming language, which means that Java programs consist of objects that can either act alone or interact with one another.
Think of it this way-the world is full of objects (people, trees, dogs, food, electronics, houses, etc.). Each of these objects can perform several actions, and each action affects some of the other objects in the world. For instance, people buy and eat food, while dogs pee on trees.
Simple enough, right? Let’s say we had a program that analyzes the inflow and outflow of animals at an animal shelter. The program would have an object to represent each dog or cat that enters or exits the shelter as well as objects for cages, people that surrender/adopt animals, and so on.
Every object has characteristics, or attributes. Using the aforementioned shelter example, animals have names, ages, genders, breeds, medical conditions, and so on.
The values of an object’s attributes give the object a state. For instance, a dog can be named Spots, be 10 years old, male, and a Chocolate Labrador. The things an object can do are its behaviors; using the example I just mentioned, dogs can bark, growl, roll over, eat, sleep, poop , and so on.
In Java, each object’s behavior is defined by a method (more on that later)
Similar objects of similar data types belong to the same class, which is essentially a blueprint for creating objects. Using the dog example, all Chocolate Labradors that enter or exit the shelter would belong to the class ChocolateLabrador.
You’ll notice that the words package, public class, and public static void are all in blue. That’s because they are reserved words, which means you can’t use them as the names of objects or variables because these words are already reserved for the syntax of Java programming. Here’s a handy glossary of reserved words-Reserved Words in Java.
One of the first methods programmers learn is the public static void main (String [] args) method; this method is essentially the entry point for Java programs. This method contains the block of code that will perform a certain action-in this case, display our output. Let’s break it down word by word:
public-meaning any object (whether in the same class or different classes) can use the main method
static-means the method belongs to its class and not to any particular object
void-meaning the method doesn’t return any value
main-this is just the name of the method
String [] args-meaning the necessary parameters for this method are arrays of Strings; arrays are just linear arrangements of things. I’ll cover arrays more in a future lesson.
public class means the class can be called from anywhere. For instance, if you were trying to create a game with several different character classes, then provided that your game class is public, the class (and any public methods) can be called by any of the character classes.
System.out.println(string) is one of the first lines of code beginner Java programmers learn. This line of code prints out whatever is in the parentheses (which is usually a string). In this case, we’ll be printing out “Hello World” (I know it’s cliche, but it’s a great first Java lesson). Now let’s break down this line of code word-by-word:
System-a built-in class that contains useful methods and varaibles
out-a static variable in the System class that represents the output of your program
println-the method that prints a line of code
Now, before I go, I want to mention a little caveat when it comes to the println method-the difference between it and the print method. See, since println and print are so similarly named, you might think you can use the two methods interchangeably and get the same result. Not true-check out the pictures below for proof.
Granted, I’m trying to do the same thing in both examples (print “Hello World” twice). But in the first picture, since I used println both times, the two instances of “Hello World” are displayed one on top of the other. In the second picture, where I used print both times, the two instances of “Hello World” are displayed right next to each other without a space.
Thanks for reading, and here’s to expanding our analytical and programming knowledge in 2019!