Pages

Saturday, May 25, 2024

Operators in Java

Java operators with examples:

1. Arithmetic Operators:

+ (Addition): Adds two operands.

int a = 5;

int b = 3;

int sum = a + b; // sum will be 8


- (Subtraction): Subtracts the second operand from the first.

int difference = a - b; // difference will be 2


* (Multiplication): Multiplies two operands.

int product = a * b; // product will be 15


/ (Division): Divides the first operand by the second. Be aware of integer division which truncates decimals.

int quotient = a / b; // quotient will be 1 (integer division)

double dQuotient = (double) a / b; // dQuotient will be 1.6666... (cast to double for decimal result)


% (Modulo): Returns the remainder after division.

int remainder = a % b; // remainder will be 2 (5 divided by 3 leaves a remainder of 2)


2. Assignment Operators:

= (Assignment): Assigns a value to a variable.

int x = 10;

Compound assignment operators (e.g., +=, -=, *=, /=): Combine assignment with the specified operation.

x += 5; // equivalent to x = x + 5; (x becomes 15)

x *= 2; // equivalent to x = x * 2; (x becomes 30)


3. Relational Operators:

== (Equal to): Checks if two operands are equal.

boolean isEqual = a == b; // isEqual will be false

!= (Not equal to): Checks if two operands are not equal.

boolean isNotEqual = a != b; // isNotEqual will be true

< (Less than): Checks if the first operand is less than the second.

boolean isLessThan = a < b;  // isLessThan will be false

> (Greater than): Checks if the first operand is greater than the second.

boolean isGreaterThan = a > b; // isGreaterThan will be true

<= (Less than or equal to): Checks if the first operand is less than or equal to the second.

boolean isLessOrEqualTo = x <= 30; // isLessOrEqualTo will be true (x is 30)

>= (Greater than or equal to): Checks if the first operand is greater than or equal to the second.

boolean isGreaterThanOrEqualTo = x >= 20; // isGreaterThanOrEqualTo will be true (x is 30)


4. Logical Operators:

&& (Logical AND): Returns true if both operands are true, false otherwise.

boolean isEven = (x % 2 == 0) && (x > 0); // isEven will be false (x is even but not greater than 0)

|| (Logical OR): Returns true if at least one operand is true, false otherwise.

boolean isPositive = (x > 0) || (x == 0); // isPositive will be true (x is positive)

! (Logical NOT): Inverts the logical state of the operand.

boolean isNegative = !isPositive; // isNegative will be false (x is positive)l

These are just a few examples. Java offers a variety of operators for different purposes.

What is Java identifier ?

In Java, an identifier is a fancy name for a custom label you give to things like variables, methods, classes, and even labels within your code. These labels are critical for differentiating between different parts of your program and and and and making your code readable.

Here's what makes a valid Java identifier:

Characters: Identifiers can only contain letters (uppercase and lowercase), digits (0-9), the dollar sign ($), and the underscore (_).

Starting character: The first character of an identifier must be a letter or one of the special characters ($ or _). Digits cannot be the first character.

Case sensitivity: Java identifiers are case-sensitive. This means that myVariable and MyVariable are considered different identifiers.

No keywords: You cannot use reserved keywords (like int, for, if, etc.) as identifiers.

Here are some examples of valid Java identifiers:

name

customerAge

total_cost

$temporaryValue (using dollar sign)

And some examples of invalid identifiers:

1stPlace (cannot start with a number)

if (reserved keyword)

my-variable (hyphens are not allowed)

By following these rules, you can create clear and meaningful identifiers that improve the readability and maintainability of your Java code.

Tuesday, May 7, 2024

Data Types in Java

 In Java, there are several data types, which can be categorized into two main categories: primitive data types and reference data types.


1. Primitive Data Types: These are basic data types predefined by the language and are not objects. They store simple values.

a. Numeric Types:

  • Integer Types: byte, short, int, long
  • Floating-point Types: float, double

b. Character Type: char

c. Boolean Type: boolean

2. Reference Data Types: These are objects that are created using predefined classes or user-defined classes. They hold references to the memory location where the data is stored.

Now, let's represent this information with a diagram:



                                      

In this diagram, you can see the two main categories of data types in Java: primitive data types and reference data types. Each category contains specific data types with their respective subtypes.