The course is part of this learning path
In this course, we'll learn about Java data types and operators.
Learning Objectives
- Variables
- Data Types
- Type Conversion & Type Casting
- Operators
- Operator Precedence
- Expressions, Statements and Block
Intended Audience
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
Prerequisites
- No prior knowledge is required about the Java programming language.
- Basic computer knowledge
Hi there. In this video, we are starting with the main fundamentals of the Java programming language. One of them is variables. In this video, first we will learn what is a variable? Then, we will learn how to declare and initialize variables. Then, we will be learning types of variables: instance variables, static variables and local variables. Then, we will be learning Java keywords. Firstly, we are starting with the 'what is a variable?' question. A variable can be thought of as a container or a box which holds value for you during the life of a Java program.
This means that when you create a variable, you reserve some space in memory. Every variable is assigned a data type and it holds a quantity of value. We have two steps to use a variable in a program: declaration and initialization. In order to declare a variable, you must specify the data type and give a unique name to the variable. In our example, we declare the variable age with the integer, shortly, int data type. We will cover data types in the next lecture.
In order to initialize a variable, you must assign it a valid value according to its data type. Using the equation sign means that the value on the right side will be saved into the variable on the left side. In our example, we assign the value 35 to the variable age. In order to declare more than one variable, use a comma to separate identifiers. Java programming language has rules and conventions for naming variables. While declaring variables, note that Java is a case-sensitive programming language, which means that the upper or lowercase of letters in your Java programs matter. So, variable age that consists of all lowercase are different from variable age that consists of the first capital letter and other lowercase.
A variable name can begin with a letter, dollar mark or underscore. If you declare a one word variable name use all lowercase letters. This is not mandatory. If you declare a variable name having more than one word, use all lowercase letters for the first word and capitalize the first letter of another word. Now, we will learn the types of variables. We have three types of variables in Java. The first one is instance variables. Instance variables are defined in a class but outside a method declaration.
They are also defined without the static keyword. The second one is static variables. Static variables are initialized only once; at the start of the program execution. These variables should be initialized first before the initialization of any instance variables. Also, they are defined in a class but outside a method declaration. We'll cover the static keyword in detail in the next lectures. The last one is local variables. Local variables are declared inside the body of a method. Also, we'll cover the method subject in detail in the next lectures. Now, let's look at the Java keywords. Like other programming languages, Java has keywords and reserved words.
We cannot use them as names for variables, methods, classes or any other identifier. You can see Java keywords on the table. Each keyword has a specific meaning in the language. We'll do some practice with these keywords in the next lectures. Let's start coding. Let's start with variable examples. In the exercise project right click on the source folder and select New Class, specify package name as variable, and class name as variable, and select the checkbox to add the main method. Click the finish button. The eclipse will generate a Java class. Let's start coding. Let's declare three variables with int type.
Firstly, a and assign 10, second, b and assign 20, and last c and assign a plus b. Let's print variable c on the screen by using the println method. Okay, let's run the code. Right click on 'Variables.Java' and select from the context menu 'Run As' In Java application. The result will print 30 in the console. Also, we can define variables that are separated by a comma. Let's run the code again. And as you can see, the result is the same. Also, we can directly write a plus b to the output. Let's run the code again by clicking this play icon. But there is a problem. The compiler considers this statement as a string and concatenates them. It wrote the numbers 10 and 20 side by side instead of 30.
In order to solve this problem, we must use a plus b between parentheses. Let's run again. And now the result is correct. Let's look at naming rules for variables. We will study variable age. Int age is valid, int _age is valid, int age3 is valid, but int a ge is invalid, and 3age is invalid because variable names cannot use space and a variable name cannot begin a digit. Also, we cannot use Java keywords as a variable name. So, int break is invalid because the break is a keyword, int do is invalid because do is a keyword, and int public is invalid because the public is a keyword.
As a result, the convention however, is to always begin your variable names with a letter, dollar sign or underscore. Additionally, the dollar sign character by convention is never used at all. You may find some situations where auto generated names will contain the dollar sign, but your variable name should always avoid using it. A similar convention exists for the underscore character. While it's technically legal to begin your variables name with underscore this practice is discouraged. White space is not permitted. Subsequent characters may be letters, digits, dollar signs or underscore characters. Conventions and common sense apply to this rule as well.
Also, when choosing a name for your variables use full words instead of cryptic abbreviations. Doing so will make your code easier to read and understand. In many cases, it will also make your code self documenting. For example, fields named speed and gear are much more intuitive than abbreviated versions, such as S and G. Also, keep in mind that the name you choose must not be a keyword or reserved word. Yes, friends. Let's take a short break here. In the next video, we'll talk about the data types. See you in the next video.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.