1. Home
  2. Explore
  3. Computer Science
  4. Java Programming Fundamentals

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Java Programming Fundamentals

Speaker 1Speaker 2

Java Programming Fundamentals

19:24
0:00-19:24
  1. Simon

    Welcome to our Java foundations episode. We’ll follow the course from setting up the tools through writing a small program, understanding Java’s basic types, and using decisions and loops. The main goal is to build a foundation you can use for larger projects.

  2. Anna

    And the course is designed for beginners: it starts with the tools and basic building blocks, then gradually applies them in a mortgage calculator. Along the way, the instructor emphasizes readable code, meaningful names, and practicing rather than memorizing.

  3. Simon

    First, the tools. The Java Development Kit, or JDK, is the development environment used to build Java applications. It includes the Java compiler and other components needed to develop and run programs. The course demonstrates downloading the JDK and installing it, then installing IntelliJ IDEA Community Edition as a code editor.

  4. Anna

    IntelliJ is the editor used in the demonstrations, but the course also names NetBeans and Eclipse as popular alternatives. The important thing is to have a JDK and an editor; the editor helps write and run code, while the JDK provides the Java development tools.

  5. Simon

    A Java program is organized around classes and methods. A class is a container for related code, and a method is a function that belongs to a class. Methods can perform a task, such as sending an email, and can accept parameters to receive information.

  6. Anna

    A method declaration includes a return type, a name, parentheses for parameters, and a body in curly braces. If it returns no value, its return type is `void`. Java’s entry point is the `main` method, which the runtime calls to start the program; a typical declaration is `public static void main(String[] args)`.

  7. Simon

    Classes and methods can have access modifiers, which control where they can be accessed. `public` is commonly used in the introductory examples. Java naming conventions also help distinguish things: class names use PascalCase, with each word starting uppercase, while method and variable names use camelCase, with the first word lowercase and later words capitalized.

  8. Anna

    That’s why the class might be named `Main`, while its entry-point method is `main`. In Java, the opening brace for a method or class is conventionally placed on the same line as its declaration. Statements generally end with a semicolon, and comments—such as text beginning with `//`—are for readers and are not executed.

  9. Simon

    When creating a project in IntelliJ, the source files go in the source folder, and Java source files use the `.java` extension. A package groups related classes, much like a folder or namespace for organizing code. A common naming convention is to reverse a company’s domain name, but you don’t need to own a registered domain to use a package name.

  10. Anna

    The introductory project is a command-line application, meaning it runs through text input and output rather than a graphical interface. That keeps the first steps focused on Java itself; graphical applications introduce additional complexity.

  11. Simon

    The first program prints `Hello, world!` using `System.out.println`. `System` provides standard input and output, `out` is the output stream, and `println` prints text followed by a line break. Text such as `"Hello, world!"` is written in double quotes because it is a string literal.

  12. Anna

    The dot operator lets you access members of a class or object: in this case, `System.out.println`. IntelliJ can suggest names and complete parentheses and semicolons, and it can run the program for you. Those conveniences hide some details, so it’s useful to understand what the tools are doing underneath.

  13. Simon

    At a high level, running a Java program involves compilation and execution. The compiler, included with the JDK, translates Java source code into bytecode, commonly stored in a `.class` file. The Java Virtual Machine, or JVM, then runs that bytecode on the operating system.

  14. Anna

    That intermediate bytecode is the key to Java’s portability: the same compiled program can run on operating systems that have a compatible Java runtime. IntelliJ automates the compile-and-run steps, but you can also invoke the compiler and Java runtime from a terminal.

  15. Simon

    Java has several editions aimed at different kinds of development. Java Standard Edition is the core platform used in this course. The course also introduces Enterprise Edition for large, distributed systems, Micro Edition as a platform aimed at mobile devices, and Java Card for smart cards.

  16. Anna

    Those names help explain the course’s scope: this first stage is about core Java fundamentals. The broader series then moves into object-oriented programming, useful parts of the standard library, and more advanced subjects such as streams, threads, and database programming.

  17. Simon

    Let’s move into the fundamentals with variables. A variable is a named place to keep data while a program runs. For example, `int age = 30;` declares an integer variable named `age` and initializes it with the value 30.

  18. Anna

    The type tells Java what kind of value the variable can hold; the identifier is its name; and `=` is the assignment operator. Initializing a variable means giving it its first value. You can later assign it a new value, or copy one variable’s value into another.

  19. Simon

    Use descriptive names such as `viewsCount` or `myAge`, rather than vague names that make the code hard to understand. You can declare multiple variables in one statement, but one declaration per line is often easier to read.

  20. Anna

    Java has two broad categories of types: primitive types, which hold simple values, and reference types, which refer to objects. There are eight primitives. For whole numbers, `byte`, `short`, `int`, and `long` use progressively more storage and support progressively larger ranges.

  21. Simon

    For decimal numbers, use `float` or `double`; `double` is the usual choice when you need more precision. Java treats a decimal literal as a `double` by default, so a `float` literal needs an `F` suffix. A large integer literal intended as a `long` can use an `L` suffix.

  22. Anna

    The other primitives are `char`, for one character in single quotes, and `boolean`, which holds `true` or `false`. Java reserves keywords such as `class`, `public`, and `void`, so they can’t be used as names for your variables or classes.

  23. Simon

    Reference types represent objects, such as a date or a point. A class is a blueprint for objects, and an object is an instance of a class. Reference variables hold references to those objects, rather than the object’s contents directly.

  24. Anna

    That distinction affects assignment. With a primitive, copying a variable copies its value, so changing one variable doesn’t change the other. With a reference, copying the variable copies the reference. Both variables can then refer to the same object, so a change made through one reference can be visible through the other.

  25. Simon

    You can use the dot operator to access an object’s fields and methods. Packages organize related classes and avoid naming conflicts; when a class is in another package, you may need an `import` statement. Common Java library classes in `java.lang`, including `String`, are available without an explicit import.

  26. Anna

    Strings are reference types, even though Java gives them a convenient way to write and initialize them: `String message = "Hello, world!";`. You can join strings with `+`, ask for their length, check whether they start or end with certain text, find an index, replace text, change case, or trim whitespace.

  27. Simon

    A key string property is immutability: string methods that appear to change a string return a new string rather than modifying the original. For example, `replace` returns a string with the replacement, while the original variable still refers to its original string unless you assign the result back.

  28. Anna

    When a string needs special characters, escape sequences help. Use `\"` to include a double quote, `\\` for a backslash, `\n` for a newline, and `\t` for a tab. For example, Windows-style paths need escaped backslashes when written as Java string literals.

  29. Simon

    Arrays store a fixed-size list of values. For example, `int[] numbers = new int[5];` creates space for five integers, and `numbers[0]` refers to the first element because array indexes start at zero. An invalid index causes an error at runtime.

  30. Anna

    If you already know the values, you can use an initializer such as `int[] numbers = {2, 3, 5, 1, 4};`. The array’s `length` field gives the number of elements. Arrays have fixed length, so if you need a structure that can grow or shrink, you’ll want a collection instead.

  31. Simon

    The `Arrays` utility class provides operations such as sorting and converting a one-dimensional array to a readable string with `Arrays.toString`. For a multidimensional array, use `Arrays.deepToString` to display the nested contents. A two-dimensional array is an array of arrays, which can represent rows and columns.

  32. Anna

    Array elements receive default values when the array is created: numeric elements start at zero, booleans at `false`, and reference-type elements at `null`. That’s different from a string containing no characters, so it’s useful to keep the distinction between an empty string and no object reference in mind.

  33. Simon

    Sometimes a value should never change after it is set. Declare it with `final` to make it a constant, then give it its value. By convention, constant names are written in uppercase with underscores between words, such as `MONTHS_IN_YEAR`.

  34. Anna

    Next are arithmetic expressions: addition, subtraction, multiplication, division, and remainder. If you divide two integers, Java performs integer division, so a fractional part is discarded. If you need a decimal result, use floating-point values or convert an operand to a floating-point type.

  35. Simon

    The increment and decrement operators add or subtract one. Their position matters in an expression: postfix, as in `x++`, uses the old value before incrementing, while prefix, as in `++x`, increments first and then uses the new value.

  36. Anna

    For adding or subtracting a larger amount, compound assignment operators make the code shorter: `x += 2` is equivalent to `x = x + 2`, and there are similar forms for subtraction, multiplication, and division. Remember operator precedence too: multiplication and division happen before addition and subtraction, unless parentheses change the order.

  37. Simon

    Type conversion can happen implicitly when Java can convert a value without losing information—for example, a smaller whole-number type can be widened to a larger one. Converting a less precise type to a more precise one, such as an integer to a double, can also happen automatically.

  38. Anna

    When a conversion could lose information, you may need an explicit cast, such as `(int) someDouble`. That discards the fractional part. A cast is for compatible types; to turn text into a number, use a parsing method instead, such as `Integer.parseInt(text)` or `Double.parseDouble(text)`. Invalid text can cause an exception.

  39. Simon

    That parsing step matters because interface input commonly arrives as text, even when the user intends to enter a number. The course also introduces the `Math` class for operations such as rounding, finding a ceiling or floor, choosing a minimum or maximum, and generating a random decimal between zero and one.

  40. Anna

    You can scale a random value—for example, multiplying by 100 to get a value in the range from zero up to 100—and then round or cast if an integer is wanted. Parentheses matter when casting: make sure the cast applies to the full expression you intend, not just an earlier part of it.

  41. Simon

    For display, `NumberFormat` can format a number as currency or as a percentage. The class is abstract, so the course uses factory methods such as `getCurrencyInstance` and `getPercentInstance` to obtain a formatter. Calling `format` produces a string, and chaining methods can keep a short expression together.

  42. Anna

    To read input from the terminal, use a `Scanner` created with `System.in`. It has methods such as `nextInt`, `nextDouble`, and `nextLine`. The `next` method reads one token, so a full name with spaces needs `nextLine`; trimming the result can remove leading and trailing whitespace.

  43. Simon

    The course applies these ideas in a mortgage calculator. It asks for a principal, an annual interest rate, and a term in years, then calculates the monthly payment and formats it as currency. The annual rate must be converted to a monthly decimal rate: divide the entered percentage by 100 and then by 12.

  44. Anna

    The number of payments is the term in years multiplied by 12. The formula uses the principal, the monthly interest rate, and a power calculation from `Math`. The course recommends named constants for values such as 12 months per year and 100 percent, rather than unexplained numbers scattered through the code.

  45. Simon

    Choosing types thoughtfully is part of making that calculator clear. The principal can use an integer type suitable for the values the program is meant to handle, the interest rate can use a floating-point type, and the number of years can use a small integer type if its supported range is enough. Names such as `annualInterestRate` communicate intent.

  46. Anna

    The initial calculator still needs input validation. If the user enters text where a number is expected, parsing can fail; if they enter a negative or out-of-range value, the calculation may not make sense. The next step is to check inputs and ask again when they’re invalid.

  47. Simon

    Control flow begins with comparison operators: equal to, not equal to, greater than, greater than or equal to, less than, and less than or equal to. These produce boolean expressions—results that are either true or false. Don’t confuse `==`, used for comparison, with `=`, used for assignment.

  48. Anna

    Logical operators combine or reverse boolean conditions. `&&` means both conditions must be true, `||` means at least one must be true, and `!` reverses a boolean. For example, a loan rule might require high income or good credit, while also requiring that the applicant not have a criminal record.

  49. Simon

    An `if` statement lets a program choose what to do. It can have an `if` clause, optional `else if` clauses, and an optional `else` clause. A condition that is true runs its associated statement or block; if none of the conditions match, the `else` branch runs.

  50. Anna

    Braces are needed when a branch contains multiple statements. The course shows that formatting the clauses consistently makes the structure easier to scan. It also demonstrates simplifying a boolean assignment: instead of an `if` that assigns `true` or `false`, assign the boolean expression itself.

  51. Simon

    When a condition selects between two values, the ternary operator can be useful. It has the form `condition ? valueIfTrue : valueIfFalse`. For instance, it can choose between a first-class and economy label without writing a longer `if` and `else`.

  52. Anna

    A `switch` statement is another way to choose behavior based on one value. It uses `case` labels and, commonly, `break` to leave the switch after a matching case. A `default` branch handles values not covered by the listed cases.

  53. Simon

    The course then uses the classic FizzBuzz exercise: print `Fizz` for numbers divisible by five, `Buzz` for numbers divisible by three, `FizzBuzz` for numbers divisible by both, and otherwise print the number. The remainder operator, `%`, checks divisibility: a remainder of zero means the number divides evenly.

  54. Anna

    The order of conditions matters. Test the most specific case—divisible by both—before the broader cases for five or three. Otherwise, a number like 15 would match the first broader condition and never reach the `FizzBuzz` case.

  55. Simon

    Loops repeat code. A `for` loop is convenient when you know how many repetitions you need. It puts initialization, a continuing condition, and an update—often incrementing a counter—in one place. A counter starting at zero and continuing while it is less than five runs five times.

  56. Anna

    A `while` loop is useful when you don’t know in advance how many repetitions are needed. For example, keep asking for input until the user types `quit`. Since strings are objects, compare their contents with a string method such as `equals`, rather than relying on `==` to compare the text.

  57. Simon

    A `do-while` loop checks its condition after running the body, so its body executes at least once. A regular `while` loop checks first and may run zero times. The course notes that `while` is the more common choice, but `do-while` fits cases where the first action must happen.

  58. Anna

    Two loop-control statements are handy: `break` exits the loop, and `continue` skips the rest of the current iteration and goes back to the loop condition. A loop such as `while (true)` can be used for repeated prompting, but it needs a reachable `break` so it can end.

  59. Simon

    For arrays and collections, a for-each loop gives you each item in turn without managing an index. It’s simpler when all you need is the value of each element. A traditional `for` loop is more suitable if you need the index or want to move backward through an array.

  60. Anna

    For the mortgage calculator’s validation, one straightforward approach is to put each prompt in a loop. Read the value, check that it’s in the permitted range, and break out only when it’s valid; otherwise, display an error and ask again. This turns validation rules into explicit program behavior.

  61. Simon

    There’s a design issue to notice as the calculator grows: putting every prompt, validation rule, and calculation inside `main` makes it long and harder to maintain. The course’s next clean-coding step is to break the code into smaller, easier-to-understand pieces.

  62. Anna

    That leads to the broader lesson: readable code is for people as well as computers. Use descriptive names, avoid magic numbers, and choose a structure that makes the rules easy to follow. Sometimes solutions involve trade-offs—for example, a little repetition may be preferable to deeply nested conditions if it makes the logic clearer.

  63. Simon

    The course roadmap builds from these basics into data types and a mortgage-calculator project, then control flow and validation, clean coding, and debugging and packaging. Later parts cover object-oriented programming, standard Java APIs, and advanced topics. The fundamentals here—types, expressions, input, decisions, and repetition—are the groundwork for all of that.

  64. Anna

    So the big picture is: install the JDK and an editor, understand how classes and the `main` method organize a program, and learn how source becomes bytecode that the JVM runs. Then use variables, types, arrays, methods, and control flow to turn a problem into a working program. Practice those ideas in the calculator, and keep asking whether someone else could understand your code.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent