Java Basics Flashcards
Writing your first Java program The different parts of a Java program Printing text to the console Organizing your code using methods How Java views and stores basic types of data Different methods of manipulating data How to create programs that allow a user to interact with the software
What are some key features of Java?
- Object Oriented
- Compiled
- Memory management
What is a compiler?
The part of Java that translates your source code into machine code
What are the steps in creating a new .java file in your “IDE” (Integrated Development Environment)?
- Project Name - This is what you enter when you are creating a new project with the IDE dialog boxes. This will be the name of the folder where all your different project files will be kept. For example, you might call your project “Java practice” and put all your different code files in one project.
- Java File Name - this is the name of the actual file that will hold your code. It will be stored in the source or “src” folder within your IDE project. This should follow the below java naming conventions.
- Class Name - this is the name of the class that you declare in your code. Java requires that this name exactly match the name of the .java file. In fact, the IDE will automatically name your class for you based on what you entered for the file name.
- Java Naming Conventions As you create new Java files and Classes you must give each a unique name. Java has its own “conventions” or suggested formats for how to create these names: No spaces Must start with a letter Capitalize the first letter of each new word i.e. HelloWorld
What are “Key Words” in Java?
You’ll know if you’ve written code correctly because certain words should change colors within your IDE. These are called “key words” and your IDE should highlight them to make it easier for you to read your code.
What is a class?
The class is the outermost layer of the program. It starts with the keywords “public class” and then whatever the name of your file is.
public class YourClass {
The name of your class has to exactly match the name of your file, including capitalization. The class is enclosed by a set of curly braces or “{ }” characters. All code you want to run should be placed between these two characters.
What is the main method?
The main method is the wrapper that tells your computer which lines of code to run. In Java it has a very specific series of keywords:
public static void main(String[] args) {
After the open curly brace, you should put each line of code you want the computer to run. Don’t forget to add the closing curly brace after all your code. As you can see in the image it is convention to add a layer of indentation between the main method and the class, this shows that the main method is inside the class.
What is a statement?
The statements are the lines of code for the computer to run. Each line is called a “statement” and should end with a semi-colon(;). You can have as many statements as you want, the computer will run each line from top to bottom one after the other. Again, as these are inside the main method all statements should be one level of indentation in.
What happens if you forget to add a semi-colon at the end of a line of code?
The computer reports a “compiler error”
What are comments?
Comments are notes written in the source code by the programmer to describe or clarify the code. They are ignored by the Java compiler so they are intended only as messages to the humans that will read the code files.
When is it recommended to add comments?
It is recommended that you add comments…
- At the top of any Java class to describe its overall function
- At the top of a “method” or a small subsection of code to help explain its contribution to the overall solution
- To explain what is happening when you have particularly complicated bits of code
- To isolate lines of code when looking for issues without having to delete code
There are two different ways to add comments:
Single line comments
// comment text, on one line
Multi line comments
/* comment text You can have as many lines as you like Between these two indicators */
What is a string?
In Java, a String is any sequence of characters – letters, digits, spaces, punctuation, and other “special characters”. Here are some examples of Strings defined in Java:
“Hello, world”: This string has 12 characters. The space between the comma and “w” is also counted as a character. The double quotes are not part of the String, they define where the String starts and where it ends.
“12234”: This is a String, not an integer number because it’s enclosed in double quotes.
”><}{_^$#@^”: This is a String with 10 special characters in it.
””: This is a special String: an “empty String” that contains NO characters at all. The two double quotes are right next to each other, without even a space between them.
What is an escape sequence?
What if you want to create a String that contains a double quote character? Tricky! If you wrote “””, Java would be confused. The computer can’t tell if “”” is an empty String followed by a double quote, or a String consisting of a double quote character. Java includes a way for you to clarify this ambiguity with what is called an “escape sequence”. When Java sees a backslash in a String this tells Java to “escape” or skip the next character’s function and simply add it to the String:
What are Printlns?
To produce String based output we use the println statement. This is a type of computer “output”. The place you look on your screen for text output from a Java program is called the “console”
Java prints text out to the console using a System.out.println statement. Here’s the one you used in your HelloWorld program:
System.out.println(“Hello, world!”);
It’s kind of wordy, but the words do make sense in a way:
- System essentially refers to the computer as a whole
- out refers to the standard output device: the console
- println indicates that you want to “print” as a complete line (ln) of text
Our statement above will print Hello, world! to the console area on your screen. It also advances to the next line on the display, just as if you typed it and then hit the Enter (or Return) key.
You can also use the println statement to produce an empty line.
System.out.println();
What are Prints?
Sometimes you may want to use several print statements and have all the output produced on the same line. For this, you can write print instead of println. For example, these statements:
System.out.print(“Hello, “);
System.out.print(“Joe”);
System.out.print(“! Hello, “);
System.out.print(“Jane”);
System.out.println(“!”);
…would print out:
Hello, Joe! Hello, Jane!
… and advance to a new line only after the last exclamation point. That is because you can see that the first 4 calls leave off the “ln” at the end of print. These are simply “prints” instead of “print lines” and do not add the carriage return or “Enter” at the end of a line of output.
Finally, you can also print multiple lines with a single println statement by using the new line escape sequence from above:
System.out.println(“line 1\nline 2\nline3”);
… prints out these three lines:
line 1
line 2
line 3
What is procedural decomposition?
The process of breaking a task down into smaller parts is called “procedural decomposition”, and almost all programming languages provide a way to do this. Java does it using what it calls “methods”. In general, you should break your program up into methods when:
- You can break a task into distinct non-trivial subtasks in order to understand it better. For example:
Fly airplane
- Take off
- Fly
- Land
You could even break one or more subtasks down further:
Fly airplane
- Take-off
- Push back from gate
- Taxi to runway
- Increase speed until off ground
- Climb to cruise altitude
- Fly
- Land
- You have “redundant code”: unnecessarily repeated code that appears more than once in your program. It’s OK to use methods to eliminate redundancy for even a single, complicated line. That way if you ever have to change it, you only have to change it in one place!
- Your method is getting bigger than 20-30 lines of code; look for groups of statements that are related pull them out into a method.
What is the ideal relationship between a main method and methods?
Ideally, once you have organized your program with methods, your main method turns into a list of method calls, each that handle their own small piece of the task to be performed. Thus your main method turns into a sort of outline of what your code is doing, making it easier for someone reading your code to quickly understand what it does.