Code-Alongs are live experiences taught by our expert instructors designed to prepare you for concepts found in the sprint challenges. They're your opportunity to work on complex job-ready problems in a live and engaging environment.
Code-Alongs are live classes 50 minutes in length designed to offer deeper insights into learning your core competencies and are offered seven days a week in the morning, afternoon, and evening.
Learn about JavaDoc, variable declarations, and how to perform arithmetic operations in Java.
JavaDoc is a documentation generator for Java that extracts comments from Java source code and generates formatted HTML documentation. JavaDoc comments help other developers understand your code's functionality.
/**
* This is a JavaDoc comment.
* @param paramName description of the parameter
* @return description of the return value
* @throws ExceptionType description of when this exception is thrown
*/
Variables are used to store data in your programs. In Java, each variable has a specific type that determines what kind of data it can hold.
type variableName;
variableName = value;
type variableName = value;
int
- Integers (whole numbers), 32 bitsdouble
- Double precision floating point numbers, 64 bitsboolean
- true or false valueschar
- Single characters like 'A', '*', '7'float
- Single precision floating point numbers, 32 bitslong
- Integers, 64 bitsshort
- Integers, 16 bitsbyte
- Integers, 8 bitsJava supports standard arithmetic operations between numeric values.
+
- Addition (also used for String concatenation)-
- Subtraction*
- Multiplication/
- Division (be careful with integer division)%
- Modulus (remainder after division)When you divide two integers, the result is an integer (decimal part is truncated):
int result = 5 / 2; // result is 2, not 2.5
To get a floating-point result, at least one operand must be a floating-point type:
double result = 5.0 / 2; // result is 2.5
double result2 = (double) 5 / 2; // result is 2.5 using casting
// Variable declarations
int count = 10;
double price = 29.99;
String name = "Java Programming";
// Arithmetic operations
int sum = count + 5; // 15
double total = price * count; // 299.9
int remainder = count % 3; // 1 (remainder of 10/3)
// String concatenation
String message = "The total price for " + count + " items is $" + total;
Explore string manipulation and methods available for working with strings in Java.
Strings in Java are objects that represent sequences of characters. The String class provides many methods for examining individual characters, comparing strings, searching strings, extracting parts of strings, and creating copies with modified content.
// String literal
String greeting = "Hello, World!";
// Using the String constructor
String message = new String("Welcome to Java");
length()
- Returns the number of characters in the stringcharAt(int index)
- Returns the character at the specified indexsubstring(int beginIndex)
- Returns a substring starting from the specified indexsubstring(int beginIndex, int endIndex)
- Returns a substring between specified indexesindexOf(String str)
- Returns the index of the first occurrence of the specified substringtoLowerCase()
- Converts all characters to lowercasetoUpperCase()
- Converts all characters to uppercasetrim()
- Removes whitespace from both ends of the stringreplace(char oldChar, char newChar)
- Replaces all occurrences of a characterequals(Object obj)
- Compares this string to another object for equalityequalsIgnoreCase(String anotherString)
- Compares strings, ignoring caseThere are several ways to concatenate strings in Java:
// Using + operator
String fullName = firstName + " " + lastName;
// Using concat() method
String result = str1.concat(str2);
// Using StringBuilder (more efficient for many concatenations)
StringBuilder builder = new StringBuilder();
builder.append("Hello");
builder.append(" ");
builder.append("World");
String message = builder.toString();
Java provides powerful string formatting capabilities using the String.format()
method:
String formatted = String.format("Hello, %s! You have $%.2f in your account.", name, balance);
// Alternatively, using printf (for printing)
System.out.printf("Hello, %s! You have $%.2f in your account.", name, balance);
%s
- Formats strings%d
- Formats decimal integers%f
- Formats floating-point numbers%n
- Inserts a platform-specific line separator%.2f
- Formats a floating-point number with 2 decimal placesString message = "Hello, Java Programming!";
// String methods in action
int length = message.length(); // 25
char firstChar = message.charAt(0); // 'H'
String sub = message.substring(7, 11); // "Java"
boolean containsJava = message.contains("Java"); // true
String lower = message.toLowerCase(); // "hello, java programming!"
String modified = message.replace('a', 'A'); // "Hello, JAvA ProgrAmming!"
// Finding occurrence
int indexOfJava = message.indexOf("Java"); // 7
The best Code-Along experiences happen when you are ready before coming to class. Your instructors created a starting point and a solution for each of your Code-Alongs to ensure you have what you need to succeed.
Follow these tips to maximize your learning during code-along sessions: