Java 101-2 (Learning basic terminology)
Table of contents
Terminology📚💻
- Identifiers - used for identification purposes. Examples of Identifiers are class, method, variable names etc.
(Note - Img from GFG, for more detail on identifier naming rule, click here)
- Reserved Words - Words reserved to represent specific utility by a programming language. Also, these words are a combination of keywords(which define utility) and literal(which define the value).
- Variables - are the names of memory locations that can be changed during program execution. It can be initialized by declaring 3 components which are
Data_type + NameOfVariable = Value;
Now value can be assigned by variable initialization which we did above or Take Input From User.
Different Types of Variable📝🤔
Local Variable | Instance Variable | Static Variable |
Defined within a method or a code block | Defined outside a method at the class level | Defined outside a method at the class level |
Is only accessible in the method/code block where it is declared | Is accessible throughout the class | Is accessible throughout the class |
Remains in memory as long as the method executes | Remains in memory as long as the object is in memory | Remains in memory as long as the program executes |
Does not require any special keyword | Does not require any special keyword but any access specifier (private, protected or public) can be specified. Typically, private or protected is used | Requires the static keyword to be specified. In addition, any access specifier (private, protected or public) can be specified. Typically, the public is used |
Requires to be initialized before it is used | Is given a default value based on its data type, so does not require to be initialized before it is used | Is given a default value based on its data type, so does not require to be initialized before it is used. |
// Local Variables
class Localvar
{
public static void main(String[] args)
{
// Declared a Local Variable
int var = 58;
// This variable is local to this main method only
System.out.println("Local Variable: " + var);
}
}
-------------------------------------
// Instance Variables
class Instvar {
// Declared Instance Variable
public String mond;
public Blog()
{
// Default Constructor
// initializing Instance Variable
// 'this' refer to current class instance variables
this.mond = "Johnson Sterling John";
}
// Main Method
public static void main(String[] args)
{
// Object Creation
Blog name = new Blog();
// Displaying O/P
System.out.println("Blog name is: " + name.mond);
}
}
-------------------------------------
// Static variables
class Statvar {
// Declared static variable
public static String mond = "Satoshi";
public static void main(String[] args)
{
// mond variable can be accessed without object
System.out.println("Mond Name is : " + Statvar.mond);
// static int k=0;
/*above line,when uncommented,
will throw an error as static variables cannot be
declared locally.*/
}
}
Note - In Java, we can usually access a variable as long as it was defined within the same set of brackets as the code we are writing or within any curly brackets inside of the curly brackets where the variable was defined. So, In general, a set of curly brackets { } defines the scope of a Variable. For more on The Scope Of Variables, click here.
Operators🔢
Arithmetic Operator - Used to do arithmetic operations.
Unary Operator - used to increment, decrement or negate a value and only needs one operand.
Note - The difference between Post(eg - a++) and Pre(eg - ++a) Increment/Decrement is that in Post, the value is first used for computing the result and then incremented while in Pre, Value is incremented first, and then the result is computed.
Assignment Operator - used to assign value to a variable.
Note - In many cases, the assignment operator can be combined with other operators to build a shorter version of the statement called a Compound Statement. For example, instead of a \= a+5, we can write a += 5.
Relational Operators - used to check the relationship between variables and values like equality, greater than, and less than. As they return boolean results, they are extensively used in looping statements as well as conditional if-else statements.
Logical Operators - used to perform “logical AND”, “logical OR” 0perations and "Logical Not" Note - The second condition is not evaluated if the first one is false, i.e., it has a short-circuiting effect. thus used a lot in decision-making tasks.
Ternary operator: Ternary operator is a shortcut version of the if-else statement. It has three operands and hence the name Ternary.
The general syntax is -
Condition? If true: If false
Here this line says that if the condition is true follow the command written after '?' but if false follow the statement after ':'.
Example -
((a > b) ? (a > c)? a: c : (b > c)? b: c);
This line says that if,
This is how we could solve the Question of "Finding the Maximum number out of given Three numbers".
Bitwise Operator - used to perform the manipulation of individual bits of a number by performing operations like And, Or, Xor, and Complement. They can be used with any of the integer types.
Shift Operator - used to shift the bits of a number left or right and fill the void with 0 as a result.
eg. - Let, A = 0101
so A >> 1 == 0010
and A << 1 == 1010
Conclusion🔍✅
Thanks for joining me today, guys! 🙌 I hope you learned something new from my tech blog. 🤓 Until next time, take care and keep exploring the amazing world of technology! 🚀💻
If you have any kind of suggestions just comment down, and I'll catch up with you.😊🖐.