Fiveable
Fiveable

or

Log in

Find what you need to study


Light

Find what you need to study

1.1 Why Programming? Why Java?

7 min readdecember 26, 2022

Athena_Codes

Athena_Codes

Athena_Codes

Athena_Codes

Why Programming? Why Java?

Welcome to AP Computer Science A, or for short! In this guide, you will learn the basics of , 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: , 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 , 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?

is one of the mentioned earlier. It is also the second most used programming language in the world, behind Python. There are several reasons that is so popular, and here's a list from Cay Horstmann's *Core *****based on the original developers' claims:

  1. Simple As a high-level language, the syntax for is much simpler than languages like C and related languages and thus is easier to read.

  2. Object-Orientated is an object-orientated language, with its emphasis on , , and fundamental to writing code in . Object-orientated programming is code written that manipulates these , , and and is central to this course.

  3. Distributed can easily connect with the internet and file systems to easily access information either from files or APIs.

  4. Distributed can easily connect with the internet and file systems to easily access information either from files or APIs.

  5. Secure 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 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 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 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 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, 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 , it will run quicker than code that needs to be reinterpreted every time in Python.

  9. High-Performance The modern compilers of are quick and can compile large blocks of code quickly.

  10. Multithreaded 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 in order to increase its functionality such as adding new , , and . 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 include:

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 , which we’ll discuss later) are located on your computer.

The Anatomy of a Java Program

Below is the first program many students learning 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.

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 compiler compiling Main.java, and then a 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 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 there are, must have a main method in only one of the . The main method is "the master switch," the method that determines what other are called. The static means that we don't need an instance of the "Main" class (we'll learn more about instantiations and 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 . "Hello world!" is what is known in as a string literal, a string for short. A string is an object in 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 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!

Key Terms to Review (14)

AP CSA

: AP CSA stands for Advanced Placement Computer Science A. It is an advanced level course offered in high schools that focuses on teaching students the fundamentals of computer science and programming.

BlueJ

: BlueJ is an integrated development environment (IDE) primarily used for teaching introductory Java programming. It provides a simplified interface and visual tools to help beginners understand object-oriented concepts.

Classes

: Classes are user-defined data types in object-oriented programming. They serve as blueprints for creating objects by defining their attributes and methods.

High-level programming languages

: High-level programming languages are languages that are designed to be easily understood by humans. They use natural language elements and abstract away low-level details, making it easier for programmers to write code.

IntelliJ

: IntelliJ is an integrated development environment (IDE) specifically designed for Java programming. It provides a user-friendly interface and a wide range of features to help developers write, debug, and test their code efficiently.

Java

: Java is a high-level, object-oriented programming language that is widely used for developing applications and software. It is known for its platform independence, meaning that programs written in Java can run on any device or operating system with a Java Virtual Machine (JVM).

Java Virtual Machine (JVM)

: The Java Virtual Machine (JVM) is a virtual machine that executes Java bytecode. It provides the runtime environment for running Java applications and translates bytecode into machine code that can be executed by the computer's operating system.

Machine code

: Machine code is the lowest level of instructions that can be executed directly by a computer's hardware. It consists of binary digits (0s and 1s) representing specific operations performed by the processor.

Methods

: Methods are functions defined within a class that perform specific tasks or actions when called upon by an object. They can manipulate data, interact with other objects, or return values.

Objects

: Objects are instances of a class that represent real-world entities or concepts. They encapsulate data (attributes) and behavior (methods) into a single entity.

repl.it

: Repl.it is an online coding platform that allows users to write, run, and debug code in various programming languages directly from their web browser.

Syntax Error

: A syntax error is a mistake in the structure or format of a program's code that prevents it from being compiled or executed properly. It occurs when the code violates the rules and grammar of the programming language.

System.out.print()

: System.out.print() is a method in Java that allows you to display text or values on the console. It does not add a new line after printing.

Visual Studio Code

: Visual Studio Code (VS Code) is a lightweight yet powerful source code editor developed by Microsoft. It supports multiple programming languages and offers various extensions to enhance productivity during coding.

1.1 Why Programming? Why Java?

7 min readdecember 26, 2022

Athena_Codes

Athena_Codes

Athena_Codes

Athena_Codes

Why Programming? Why Java?

Welcome to AP Computer Science A, or for short! In this guide, you will learn the basics of , 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: , 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 , 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?

is one of the mentioned earlier. It is also the second most used programming language in the world, behind Python. There are several reasons that is so popular, and here's a list from Cay Horstmann's *Core *****based on the original developers' claims:

  1. Simple As a high-level language, the syntax for is much simpler than languages like C and related languages and thus is easier to read.

  2. Object-Orientated is an object-orientated language, with its emphasis on , , and fundamental to writing code in . Object-orientated programming is code written that manipulates these , , and and is central to this course.

  3. Distributed can easily connect with the internet and file systems to easily access information either from files or APIs.

  4. Distributed can easily connect with the internet and file systems to easily access information either from files or APIs.

  5. Secure 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 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 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 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 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, 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 , it will run quicker than code that needs to be reinterpreted every time in Python.

  9. High-Performance The modern compilers of are quick and can compile large blocks of code quickly.

  10. Multithreaded 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 in order to increase its functionality such as adding new , , and . 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 include:

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 , which we’ll discuss later) are located on your computer.

The Anatomy of a Java Program

Below is the first program many students learning 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.

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 compiler compiling Main.java, and then a 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 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 there are, must have a main method in only one of the . The main method is "the master switch," the method that determines what other are called. The static means that we don't need an instance of the "Main" class (we'll learn more about instantiations and 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 . "Hello world!" is what is known in as a string literal, a string for short. A string is an object in 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 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!

Key Terms to Review (14)

AP CSA

: AP CSA stands for Advanced Placement Computer Science A. It is an advanced level course offered in high schools that focuses on teaching students the fundamentals of computer science and programming.

BlueJ

: BlueJ is an integrated development environment (IDE) primarily used for teaching introductory Java programming. It provides a simplified interface and visual tools to help beginners understand object-oriented concepts.

Classes

: Classes are user-defined data types in object-oriented programming. They serve as blueprints for creating objects by defining their attributes and methods.

High-level programming languages

: High-level programming languages are languages that are designed to be easily understood by humans. They use natural language elements and abstract away low-level details, making it easier for programmers to write code.

IntelliJ

: IntelliJ is an integrated development environment (IDE) specifically designed for Java programming. It provides a user-friendly interface and a wide range of features to help developers write, debug, and test their code efficiently.

Java

: Java is a high-level, object-oriented programming language that is widely used for developing applications and software. It is known for its platform independence, meaning that programs written in Java can run on any device or operating system with a Java Virtual Machine (JVM).

Java Virtual Machine (JVM)

: The Java Virtual Machine (JVM) is a virtual machine that executes Java bytecode. It provides the runtime environment for running Java applications and translates bytecode into machine code that can be executed by the computer's operating system.

Machine code

: Machine code is the lowest level of instructions that can be executed directly by a computer's hardware. It consists of binary digits (0s and 1s) representing specific operations performed by the processor.

Methods

: Methods are functions defined within a class that perform specific tasks or actions when called upon by an object. They can manipulate data, interact with other objects, or return values.

Objects

: Objects are instances of a class that represent real-world entities or concepts. They encapsulate data (attributes) and behavior (methods) into a single entity.

repl.it

: Repl.it is an online coding platform that allows users to write, run, and debug code in various programming languages directly from their web browser.

Syntax Error

: A syntax error is a mistake in the structure or format of a program's code that prevents it from being compiled or executed properly. It occurs when the code violates the rules and grammar of the programming language.

System.out.print()

: System.out.print() is a method in Java that allows you to display text or values on the console. It does not add a new line after printing.

Visual Studio Code

: Visual Studio Code (VS Code) is a lightweight yet powerful source code editor developed by Microsoft. It supports multiple programming languages and offers various extensions to enhance productivity during coding.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.