πŸ“š

Β >Β 

πŸ’»Β 

Β >Β 

βž•

1.1 Why Programming? Why Java?

7 min readβ€’december 26, 2022

Peter Cao

Peter Cao


AP Computer Science AΒ πŸ’»

130Β resources
See Units

Why Programming? Why Java?

Welcome to AP Computer Science A, or AP CSA for short! In this guide, you will learn the basics of Java, a programming language that is widely used today.

Intro to Programming

Almost all the technology we use today, from everything on your computer to your smartwatch to your television, is made possible by programming. Programming is what makes these devices respond to your actions and commands. What is programming anyway? In short, it is giving a device instructions to respond to different actions and run. There's a lot more complexity to this involving computer hardware, but that is beyond the scope of this course. In essence, our computer is "run" using bits of 0s and 1s, which represent on and off electrical signals. Some programming languages, such as Assembly, interact almost directly with those bits. However, Assembly is inconvenient to program in and is incredibly hard to read.
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-4M8zSN6hjkw5.png?alt=media&token=62ee9ea1-7a38-4fe0-84d4-01850c7518c2

Courtesy of Android Authority

Thus, a need for higher-level programming languages that are easier to code in and understand was born. First came languages like Pascal and FORTRAN, then C, C#, and C++, common mainstays in the programming world. The most popular programming languages of Javascript, Python, and the focus of this course: Java, have only come about recently. These newer languages are much easier to understand and look like everyday English to an extent. For machines to understand this code, however, the program must be converted into machine code, which is what computers understand. As you progress, you'll learn that Each language has a different way of doing this.
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-JnZT2lFkrcnB.png?alt=media&token=501feab9-99be-4f4d-9ecc-0b6ef3389a46

Courtesy of MalwareBytes.

What Is Java, How Does It Work, and Why Do We Use It?

Java is one of the high-level programming languages mentioned earlier. It is also the second most used programming language in the world, behind Python. There are several reasons that Java is so popular, and here's a list from Cay Horstmann's *Core Java *****based on the original Java developers' claims:
  1. Simple As a high-level language, the syntax for Java is much simpler than languages like C and related languages and thus is easier to read.
  2. Object-Orientated Java is an object-orientated language, with its emphasis on classes, objects, and methods fundamental to writing code in Java. Object-orientated programming is code written that manipulates these classes, objects, and methods and is central to this course.
  3. Distributed Java can easily connect with the internet and file systems to easily access information either from files or APIs.
  4. Distributed Java can easily connect with the internet and file systems to easily access information either from files or APIs.
  5. Secure Java is built to be resistant to many different attacks, which makes sense as one of its original intents is to be connected to networks.
  6. Architecture-Neutral One of the most common sayings of Java is "Code once, run anywhere" because once it is compiled, it does not depend what device it is on, it will run, since the bytecode is run on the Java Virtual Machine (JVM) which is the same on every device. This is a contrast to earlier programming languages which will only run on the device it was coded on.
  7. Architecture-Neutral One of the most common sayings of Java is "Code once, run anywhere" because once it is compiled, it does not depend what device it is on, it will run, since the bytecode is run on the Java Virtual Machine (JVM) which is the same on every device. This is a contrast to earlier programming languages which will only run on the device it was coded on.
  8. Interpreted The JVM acts as an interpreter, which can quickly use the bytecode and return an output. However, Java is still mainly a compiled language, as opposed to Python, which is an interpreted language, where the code is immediately turned into output without any intermediary files. However, once we have the compiled code in Java, it will run quicker than code that needs to be reinterpreted every time in Python.
  9. High-Performance The modern compilers of Java are quick and can compile large blocks of code quickly.
  10. Multithreaded Java was the first modern language to support parallel programming, which is doing multiple processes, called threads at a time. This will be useful for multi-core processors allowing for stronger computers.
  11. Dynamic There are many libraries that can be added to the Java in order to increase its functionality such as adding new classes, objects, and methods. We'll be looking at a few of them in this course.

Getting Ready for Your First Program

Before we start programming, we have to actually set up our coding environment. To do this, we use an Integrated Development Environment (IDE). Your teacher will usually have a preference, but if they don't, any of these will work perfectly fine. A few well-known ones for Java include:
  • BlueJ
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-3xaqPSxXoI5Z.png?alt=media&token=a953e645-e7d4-4d92-973d-db9304d11eaa

Courtey of BlueJ.

However, in these guides, code will either be embedded directly onto the site or be from repl.it, an online IDE that takes advantage of remote compilers so that your computer's RAM isn't overwhelmed.
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-vs4eEccHbCLT.png?alt=media&token=a03a80ef-8b91-4442-9e59-6aa1b177b176

Courtesy of Repl.it.

The white half is the code editor, where you enter your code that is compiled and later executed. The blue half is the console, which shows the output of the program and any errors that occur. On the left of the screen is the file explorer (not visible above), and this is where your different code files (which are your Java classes, which we’ll discuss later) are located on your computer.

The Anatomy of a Java Program

Below is the first program many students learning Java will see. There seems to be a lot going on, but here, we'll break it down!
public class Main { public static void main(String[] args) { System.out.println("Hello world!"); } }
Do you see the indentation? Indentation isn't needed but it can make your code much easier to read! I recommend proper indentation when coding by either using a tab or 2 spaces. Just remember to be consistent!
If you hit run, the console will return:
> javac -classpath .:/run_dir/junit-4.12.jar:target/dependency/* -d . Main.java > java -classpath .:/run_dir/junit-4.12.jar:target/dependency/* Main Hello world!
There is a lot that's going on in these snippets, and we'll learn these more in-depth in later units. In the console, we first see javac, which is the Java compiler compiling Main.java, and then a java command executing the Main bytecode. The third line (and beyond, depending on the program) has the result of the code. In future instances, we won't show the first two lines.
Meanwhile, on the code, let's decode it line by line! The java files are always in the name ClassName.java, and in this case, the file in question is Main.java thus the class Main. The public means that it can be accessed from any other class. The {} denotes the contents inside the class and the main method after itβ€”in other programming languages, they are called functionsβ€”with the method header in line 2. Every program, no matter how many classes there are, must have a main method in only one of the classes. The main method is "the master switch," the method that determines what other methods are called. The static means that we don't need an instance of the "Main" class (we'll learn more about instantiations and objects in Unit 2). The void signifies that the method does not return anything directly to another function. Then, the method name is next, and any parameters in parentheses. In this case, the parameters (String[] args) is an array of strings named args (we'll learn more about arrays in Unit 6). The third line is what does some work. The System.out.println("Hello world!"); prints Hello world!, and sets the cursor to a new line if there is anything else to print. Note the semicolon after the line; this is necessary as it shows the end of an action. If it isn't there, the compiler returns a syntax error. "Hello world!" is what is known in Java as a string literal, a string for short. A string is an object in Java that sets up a template for strings. Any time there is a statement enclosed in double-quotes, that is a string.

System.out.println() vs System.out.print()

There are two ways to print text to the consoles that we'll use in this course. They are both methods of the System.out object, which is a part of the System class. System.out.println() prints whatever is in the parentheses to the console and sets the cursor on a new line. On the other hand, System.out.print() prints whatever is in the parentheses, but doesn't set the cursor on a new line.

Example

public class Main { public static void main(String[] args) { System.out.print("Hi! "); System.out.println("Hello!"); System.out.println("Bye!"); } } Hi! Hello! Bye!
Browse Study Guides By Unit
βž•Unit 1 – Primitive Types
πŸ“±Unit 2 – Using Objects
πŸ–₯Unit 3 – Boolean Expressions & if Statements
πŸ•ΉUnit 4 – Iteration
βš™οΈUnit 5 – Writing Classes
⌚️Unit 6 – Array
πŸ’ΎUnit 7 – ArrayList
πŸ’»Unit 8 – 2D Array
πŸ–²Unit 9 – Inheritance
πŸ–±Unit 10 – Recursion
πŸ™Exam Reviews

Fiveable
Fiveable
Home
Stay Connected

Β© 2023 Fiveable Inc. All rights reserved.


Β© 2023 Fiveable Inc. All rights reserved.