Java Basics #2
Java Operators
Operators are used to perform operations on variables and values.
Example:
int x = 100 + 50;
Although the +
operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable.
Example:
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)
Java divides the operators into the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
Java Assignment Operators
Assignment operators are used to assign values to variables.
Example:
Java Strings
Strings are used for storing text.
A String
variable contains a collection of characters surrounded by double quotes.
Example:
String greeting = "Hello";
String Length
A String in Java is actually an object, which contain methods that can perform certain operations on strings. For example, the length of a string can be found with the length()
method.
Example:
Java Methods
You can pass data, known as parameters, into a method.
Methods are used to perform certain actions, and they are also known as functions.
public class Main {
static void myMethod() {
// code to be executed
}
}
Example Explained
myMethod()
is the name of the methodstatic
means that the method belongs to the Main class and not an object of the Main class.void
means that this method does not have a return value.
Call a Method
To call a method in Java, write the method's name followed by two parentheses () and a semicolon;
Example:
static void myMethod() {
System.out.println("I just got executed!");
}
public static void main(String[] args) {
myMethod();
}
}
Java - What is OOP?
OOP stands for Object-Oriented Programming.
Procedural programming is about writing procedures or methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods.
Object-oriented programming has several advantages over procedural programming:
- OOP is faster and easier to execute
- OOP provides a clear structure for the programs
- OOP helps to keep the Java code DRY "Don't Repeat Yourself", and makes the code easier to maintain, modify and debug
- OOP makes it possible to create full reusable applications with less code and shorter development time.
Java Classes/Objects
Java is an object-oriented programming language.
Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.
Create a Class
To create a class, use the keyword class.
int x = 5;
}
Create an Object
In Java, an object is created from a class. We have already created the class named MyClass
, so now we can use this to create objects.
To create an object of MyClass
, specify the class name, followed by the object name, and use the keyword new.
Example:
int x = 5;
public static void main(String[] args) {
Main myObj = new Main();
System.out.println(myObj.x);
}
}
Multiple Objects
You can create multiple objects of one class.
Java Class Attributes
In the previous , we used the term "variable" for x
in the example. It is actually an attribute of the class. Or you could say that class attributes are variables within a class.
Static vs. Non-Static
You will often see Java programs that have either static
or public
attributes and methods.
Java User Input
The Scanner
class is used to get user input, and it is found in the java.util
package.
Example:
String userName;
String userName = myObj.nextLine();
Comments
Post a Comment