java
18 flashcards · Programming Languages · by Daniel Blackbird
print hello world public class FirstJavaProgram { public static void main(String[] args){ System.out.println("Hello World"); }//End of main }//End of FirstJavaProgram Class
Java is a high level, modern programming language designed in the early 1990s by sun microsystems, and currently owned by oracle. Java is Platform Independent, which means that you only need to write the program once to be able to run it on a number of different platforms! Java is portable, robust, and dynamic, with the ability to fit the needs of virtually any type of application. More than 3 billion devices run Java. Java is used to develop apps for Google's Android OS, various Desktop Applic…
n Java, every line of code that can actually run needs to be inside a class. In our example, we named the class MyClass. You will learn more about classes in the upcoming modules. In Java, each application has an entry point, or a starting point, which is a method called main
[public ,static,void,main] - public: anyone can access it - static: method can be run without creating an instance of the class containing the main method - void: method doesn't return any value - main: the name of the method
Remember: do not use semicolons after method and class declarations that follow with the body defined using the curly braces.
// this is a single-line comment
/* This is also a comment spanning multiple lines */
/** This is a documentation comment */
/********************** This is the start of a method ***********************/
Variables This creates a variable called name of type String, and assigns it the value "David". String name = "David";
Variables store data for processing. A variable is given a name (or identifier), such as area, age, height, and the like. The name uniquely identifies each variable, assigning a value to the variable and retrieving the value stored. Variables have types. Some examples: - int: for integers (whole numbers) such as 123 and -456 - double: for floating-point or real numbers with optional decimal points and fractional parts in fixed or scientific notations, such as 3.1416, -55.66. - String: for texts…
String name ="David"; int age = 42; double score =15.9; char group = 'Z';
char stands for character and holds a single character. Another type is the Boolean type, which has only two possible values: true and false. This data type is used for simple flags that track true/false conditions.
While Java provides many different methods for getting user input, the Scanner object is the most common, and perhaps the easiest to implement. Import the Scanner class to use the Scanner object, as seen here: import java.util.Scanner; In order to use the Scanner class, create an instance of the class by using the following syntax: Scanner myVar = new Scanner(System.in);
You can now read in different kinds of input data that the user enters. Here are some methods that are available through the Scanner class: Read a byte - nextByte() Read a short - nextShort() Read an int - nextInt() Read a long - nextLong() Read a float - nextFloat() Read a double - nextDouble() Read a boolean - nextBoolean() Read a complete line - nextLine() Read a word - next()
import java.util.Scanner; class test { public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); String st = sc.nextLine(); } }
The Math Operators Java provides a rich set of operators to use in manipulating variables. A value used on either side of an operator is called an operand. For example, in the expression below, the numbers 6 and 3 are operands of the plus operator: int x = 6 + 3;
Java arithmetic operators: + addition - subtraction * multiplication / division % modulo
Modulo int Value = 23; int res = value % 6; //res is 5 The modulo (or remainder) math operation performs an integer division of one value by another and returns the remainder of that division. The operator for the modulo operation is the percentage (%) character.
The modulo (or remainder) math operation performs an integer division of one value by another and returns the remainder of that division. The operator for the modulo operation is the percentage (%) character.
Increment Operators public class Program { public static void main(String[] args) { int test = 5; ++test; System.out.println(test); //6 } } An increment or decrement operator provides a more convenient and compact way to increase or decrease the value of a variable by one. For example, the statement x=x+1; can be simplified to ++x; public class Program { public static void main(String[] args) { int test = 5; --test; System.out.println(test); //4 } } The decrement operator (--) is used to decrea…
Prefix & Postfix public class Program { public static void main(String[] args) { int x = 34; int y = ++x; System.out.println(y); } } Two forms, prefix and postfix, may be used with both the increment and decrement operators. With prefix form, the operator appears before the operand, while in postfix form, the operator appears after the operand. Below is an explanation of how the two forms work: Prefix: Increments the variable's value and uses the new value in the expression. The value of x is f…
Assignment Operators Addition and assignment (+=): int num1 = 4; int num2 = 8; num2 += num1; // num2 = num2 + num1; // num2 is 12 and num1 is 4 Subtraction and assignment (-=): int num1 = 4; int num2 = 8; num2 -= num1; // num2 = num2 - num1; // num2 is 4 and num1 is
Strings A String is an object that represents a sequence of characters. For example, "Hello" is a string of 5 characters. String s = "SoloLearn";
You are allowed to define an empty string. For example, String str = "";