diff --git a/public/sitemap.xml b/public/sitemap.xml index b2be25d3..ed15d9f6 100644 --- a/public/sitemap.xml +++ b/public/sitemap.xml @@ -9,52 +9,57 @@ https://javaistic.vercel.app/ - 2021-07-30T20:55:43+00:00 + 2021-08-03T13:34:31+00:00 1.00 https://javaistic.vercel.app/docs - 2021-07-30T20:55:43+00:00 + 2021-08-03T13:34:31+00:00 0.80 https://javaistic.vercel.app/docs/installation - 2021-07-30T20:55:43+00:00 + 2021-08-03T13:34:31+00:00 0.80 https://javaistic.vercel.app/programs/introduction - 2021-07-30T20:55:43+00:00 + 2021-08-03T13:34:31+00:00 0.80 https://javaistic.vercel.app/programs - 2021-07-30T20:55:43+00:00 + 2021-08-03T13:34:31+00:00 0.80 https://javaistic.vercel.app/brand - 2021-07-30T20:55:43+00:00 + 2021-08-03T13:34:31+00:00 0.80 https://javaistic.vercel.app/docs/introduction - 2021-07-30T20:55:43+00:00 + 2021-08-03T13:34:31+00:00 0.64 https://javaistic.vercel.app/docs/hello-world - 2021-07-30T20:55:43+00:00 + 2021-08-03T13:34:31+00:00 0.64 https://javaistic.vercel.app/docs/jvm-jre-jdk - 2021-07-30T20:55:43+00:00 + 2021-08-03T13:34:31+00:00 0.64 https://javaistic.vercel.app/docs/variables-and-literals - 2021-07-30T20:55:43+00:00 + 2021-08-03T13:34:31+00:00 + 0.64 + + + https://javaistic.vercel.app/docs/variables-primitive-data-types + 2021-08-03T13:34:31+00:00 0.64 diff --git a/src/components/home/Footer.js b/src/components/home/Footer.js index fc72c970..2f45f5e7 100644 --- a/src/components/home/Footer.js +++ b/src/components/home/Footer.js @@ -29,7 +29,7 @@ const footerNav = { className: 'row-span-2', items: [ { title: 'Brand', href: '/brand' }, - { title: 'Change Log', href: 'https://github.com/javaistic/javaistic/releases' }, + { title: 'Changelog', href: 'https://javaistic-changelog.vercel.app/' }, { title: 'Open Source', href: '/' }, { title: 'Contact', href: 'mailto:javaistic@gmail.com' }, ], diff --git a/src/layouts/SidebarLayout.js b/src/layouts/SidebarLayout.js index 9d7872c5..f6e9be60 100644 --- a/src/layouts/SidebarLayout.js +++ b/src/layouts/SidebarLayout.js @@ -199,7 +199,7 @@ function TopLevelNav() { Programs } > - Change Log + Changelog - + ` operator is the relational operator. It checks if `a` is less than `b` or not. + +It returns either true or false. + +| Operator | Description | Example | +| :------: | :---------: | :------: | +| == | Is Equal To | 3 == 5 returns false | +| != | Not Equal To | 3 != 5 returns true | +| > | Greater Than | 3 > 5 returns false | +| < | Less Than | 3 < 5 returns true | +| >= | Greater Than or Equal To | 3 >= 5 returns false | +| <= | Less Than or Equal To | 3 <= 5 returns true | + +#### Example 3: Relational Operators + +```java +class Main { + public static void main(String[] args) { + + // create variables + int a = 7, b = 11; + + // value of a and b + System.out.println("a is " + a + " and b is " + b); + + // == operator + System.out.println(a == b); // false + + // != operator + System.out.println(a != b); // true + + // > operator + System.out.println(a > b); // false + + // < operator + System.out.println(a < b); // true + + // >= operator + System.out.println(a >= b); // false + + // <= operator + System.out.println(a <= b); // true + } +} +``` +
+ + + +**Note**: Relational operators are used in decision making and loops. + + + +## 4. Java Logical Operators +Logical operators are used to check whether an expression is true or false. They are used in decision making. + + + + + + + + + + + + + + + + + + + + + + + + + + +
OperatorExampleMeaning
&&(Logical AND)expression1 && expression2 true only if both expression1 and expression2 are true
||(Logical OR)expression1 || expression2true if either expression1 or expression2 is
!(Logical NOT)!expressiontrue if expression is false and vice versa
+ +#### Example 4: Logical Operators + +```java +class Main { + public static void main(String[] args) { + + // && operator + System.out.println((5 > 3) && (8 > 5)); // true + System.out.println((5 > 3) && (8 < 5)); // false + + // || operator + System.out.println((5 < 3) || (8 > 5)); // true + System.out.println((5 > 3) || (8 < 5)); // true + System.out.println((5 < 3) || (8 < 5)); // false + + // ! operator + System.out.println(!(5 == 3)); // true + System.out.println(!(5 > 3)); // false + } +} +``` + +#### Working of Program + +- (5 > 3) && (8 > 5) returns true because both (5 > 3) and (8 > 5) are true. +- (5 > 3) && (8 < 5) returns false because the expression (8 < 5) is false. +- (5 < 3) || (8 > 5) returns true because the expression (8 > 5) is true. +- (5 > 3) && (8 > 5) returns true because the expression (5 > 3) is true. +- (5 > 3) && (8 > 5) returns false because both (5 < 3) and (8 < 5) are false. +- !(5 == 3) returns true because 5 == 3 is false. +- !(5 > 3) returns false because 5 > 3 is true. + +## 5. Java Unary Operators +Unary operators are used with only one operand. For example, ++ is a unary operator that increases the value of a variable by 1. That is, ++5 will return 6. + +Different types of unary operators are: + +| Operator | Meaning | +| :------: | :------: | +| + | Unary plus: not necessary to use since numbers are positive without using it | +| - | Unary minus: inverts the sign of an expression | +| ++| Increment operator: increments value by 1 | +|-- | Decrement operator: decrements value by 1 | +| ! | Logical complement operator: inverts the value of a boolean | + +### Increment and Decrement Operators +Java also provides increment and decrement operators: **++** and **- -** respectively. **++** increases the value of the operand by **1**, while **- -** decrease it by **1**. For example, + +```java +int num = 5; + +// increase num by 1 +++num; +``` +Here, the value of `num` gets increased to **6** from its initial value of **5**. + +#### Example 5: Increment and Decrement Operators + +```java +class Main { + public static void main(String[] args) { + + // declare variables + int a = 12, b = 12; + int result1, result2; + + // original value + System.out.println("Value of a: " + a); + + // increment operator + result1 = ++a; + System.out.println("After increment: " + result1); + + System.out.println("Value of b: " + b); + + // decrement operator + result2 = --b; + System.out.println("After decrement: " + result2); + } +} +``` +#### Output + +```text +Value of a: 12 +After increment: 13 +Value of b: 12 +After decrement: 11 +``` +In the above program, we have used the ++ and - - operator as **prefixes (++a, - -b)**. We can also use these operators as **postfix (a++, b++)**. + +There is a slight difference when these operators are used as prefix versus when they are used as a postfix. + + +## 6. Java Bitwise Operators +Bitwise operators in Java are used to perform operations on individual bits. For example, + +```text +Bitwise complement Operation of 35 + +35 = 00100011 (In Binary) + +~ 00100011 + ________ + 11011100 = 220 (In decimal) +``` + +Here, `~` is a bitwise operator. It inverts the value of each bit (**0** to **1** and **1** to **0**). + +The various bitwise operators present in Java are: + +| Operator | Description | +| :------: | :------: | +| ~ | Bitwise Complement | +| << | Left Shift | +| >> | Right Shift | +|>>> | Unsigned Right Shift | +| & | Bitwise AND | +| ^ | Bitwise exclusive OR | + +These operators are not generally used in Java. To learn more, visit [Java Bitwise and Bit Shift Operators](/bitwise-and-bit-shift-operators-in-java). + +## 7. Other operators +Besides these operators, there are other additional operators in Java. + +### Java instanceof Operator +The `instanceof` operator checks whether an object is an instanceof a particular class. For example, + +```java +class Main { + public static void main(String[] args) { + + String str = ""; + boolean result; + + // checks if str is an instance of + // the String class + result = str instanceof String; + System.out.println("Is str an object of String? " + result); + } +} +``` +Output + +```text +Is str an object of String? true +``` +Here, str is an instance of the String class. Hence, the instanceof operator returns true. To learn more, visit Java instanceof. + +### Java Ternary Operator +The ternary operator (conditional operator) is shorthand for the if-then-else statement. For example, + +```java +variable = Expression ? expression1 : expression2 +``` +Here's how it works. + +- If the `Expression` is `true`, `expression1` is assigned to the `variable`. +- If the `Expression` is `false`, `expression2` is assigned to the `variable`. +Let's see an example of a ternary operator. + +```java +class Java { + public static void main(String[] args) { + + int februaryDays = 29; + String result; + + // ternary operator + result = (februaryDays == 28) ? "Not a leap year" : "Leap year"; + System.out.println(result); + } +} +``` +Output +```text +Leap year +``` +In the above example, we have used the ternary operator to check if the year is a leap year or not. To learn more, visit the [Java ternary operator](/ternary-operator). + +Now that you know about Java operators, it's time to know about the order in which operators are evaluated. To learn more, visit [Java Operator Precedence](/java-operator-precedence). \ No newline at end of file diff --git a/src/pages/docs/variables-primitive-data-types.mdx b/src/pages/docs/variables-primitive-data-types.mdx new file mode 100644 index 00000000..2fd639df --- /dev/null +++ b/src/pages/docs/variables-primitive-data-types.mdx @@ -0,0 +1,208 @@ +--- +title: Java Data Types +description: In this tutorial, we will learn about all 8 primitive data types in Java with the help of examples. + +--- + +import { Heading } from '@/components/Heading' +import { List, ListItemGood } from '@/components/List' +import Link from 'next/link' +import { TipInfo } from '@/components/Tip' + +## Java Data Types + +As the name suggests, data types specify the type of data that can be stored inside [variables in Java](/docs/variables-and-literals). + +Java is a statically-typed language. This means that all variables must be declared before they can be used. + +```java +int speed; +``` + +Here, `speed` is a variable, and the data type of the variable is `int`. + +The `int` data type determines that the `speed` variable can only contain integers. + +There are 8 data types predefined in Java programming language, known as primitive data types. + + + + **Note :** In addition to primitive data types, there are also referenced + types (object type). + + + +## 8 Primitive Data Types + + ### 1. boolean type + + - The `boolean` data type has two possible values, either true or false. + - They are usually used for **true/false** conditions. + - **Default value : `false`**. + + #### Example 1: Java `boolean` data type + + ```java + class Main { + public static void main(String[] args) { + boolean flag = true; + System.out.println(flag); // prints true + } + } + ``` + +### 2. byte type + + - The `byte` data type can have values from **-128** to **127** (8-bit signed two's complement integer). + - If it's certain that the value of a variable will be within -128 to 127, then it is used instead of int to save memory. + - **Default value : 0** + + #### Example 2: Java `byte` data type + + ```java + class Main { + public static void main(String[] args) { + byte range; + range = 124; + System.out.println(range); // prints 124 + } + } + ``` + +### 3. short type + + - The `short` data type in Java can have values from **-32768** to **32767** (16-bit signed two's complement integer). + - If it's certain that the value of a variable will be within -32768 and 32767, then it is used instead of other integer data types (`int`, `long`). + - **Default value : 0** + + #### Example 3: Java `short` data type + + ```java + class Main { + public static void main(String[] args) { + short temperature; + temperature = -200; + System.out.println(temperature); // prints -200 + } + } + ``` + +### 4. int type + + - The int data type can have values from **-231** to **231 -1** (32-bit signed two's complement integer). + - If you are using Java 8 or later, you can use an unsigned 32-bit integer. This will have a minimum value of 0 and a maximum value of 232-1. To learn more, visit [How to use the unsigned integer in java 8?](http://stackoverflow.com/questions/25556017/how-to-use-the-unsigned-integer-in-java-8) + - **Default value : 0** + + #### Example 4: Java `int` data type + + ```java + class Main { + public static void main(String[] args) { + int range = -4250000; + System.out.println(range); // print -4250000 + } + } + ``` + +### 5. long type + + - The `long` data type can have values from **-263** to **263 -1** (64-bit signed two's complement integer). + - If you are using Java 8 or later, you can use an unsigned 64-bit integer with a minimum value of 0 and a maximum value of 264 -1. + - **Default value : 0** + + #### Example 5: Java `long` data type + + ```java + class LongExample { + public static void main(String[] args) { + long range = -42332200000L; + System.out.println(range); // prints -42332200000 + } + } + ``` + + Notice, the use of `L` at the end of `-42332200000`. This represents that it's an integral literal of the `long` type. You will learn about integral literals later in this article. + +### 6. double type + + - The `double` data type is a double-precision 64-bit floating-point. + - It should never be used for precise values such as currency. + - **Default value : 0.0 (0.0d)** + + #### Example 6: Java `double` data type + + ```java + class Main { + public static void main(String[] args) { + double number = -42.3; + System.out.println(number); // prints -42.3 + } + } + ``` + +### 7. float type + + - The `float` data type is a single-precision 32-bit floating-point.Learn more about [single-precision and double-precision floating-point](http://stackoverflow.com/questions/801117/whats-the-difference-between-a-single-precision-and-double-precision-floating-p) if you are interested. + - It should never be used for precise values such as currency. + - **Default value : 0.0 (0.0f)** + + #### Example 7: Java `float` data type + + ```java highlight=3 + class Main { + public static void main(String[] args) { + float number = -42.3f; + System.out.println(number); // prints -42.3 + } + } + ``` + + Notice that, we have used `-42.3f` instead of `-42.3` in the above program. It's because `-42.3` is a `double` literal. + To tell the compiler to treat `-42.3` as `float` rather than `double`, you need to use `f` or `F`. + If you want to know about single-precision and double-precision, visit Java single-precision and double-precision floating-point. + +### 8. char type + + - It's a 16-bit Unicode character. + - The minimum value of the `char` data type is `'\u0000'` (0) and the maximum value of the is `'\uffff'`. + - **Default value : '\u0000'** + + #### Example 8: Java `char` data type + + ```java + class Main { + public static void main(String[] args) { + char letter = '\u0051'; + System.out.println(letter); // prints Q + } + } + ``` + + Here, the Unicode value of Q is `\u0051`. Hence, we get Q as the output. + + Here is another example: + + ```java + class Main { + public static void main(String[] args) { + char letter1 = '9'; + System.out.println(letter1); // prints 9 + char letter2 = 65; + System.out.println(letter2); // prints A + + } + } + ``` + +Here, we have assigned 9 as a character (specified by single quotes) to the letter1 variable. However, the letter2 variable is assigned 65 as an integer number (no single quotes). + +Hence, A is printed to the output. It is because Java treats characters as integral types and the ASCII value of A is 65. To learn more about ASCII, visit What is ASCII Code?. + +## String type +Java also provides support for character strings via java.lang.String class. Strings in Java are not primitive types. Instead, they are objects. For example, + +```java +String myString = "Java Programming"; +``` + +Here, myString is an object of the String class. To learn more, visit [Java Strings](/docs/string). diff --git a/src/pages/index.js b/src/pages/index.js index 41b68eff..12a87f7d 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -12,6 +12,11 @@ export default function Home() { return (
+ - + (Sponsor Javaistic) - -  Sponsor + + +  Sponsor
diff --git a/src/pages/programs/print-an-integer.mdx b/src/pages/programs/print-an-integer.mdx new file mode 100644 index 00000000..c304d8b2 --- /dev/null +++ b/src/pages/programs/print-an-integer.mdx @@ -0,0 +1,93 @@ +--- +title: Print an Integer +description: In this program, you'll learn to print a predefined number aw well as a nummber entered by the user in Java. +--- + +import { Heading } from '@/components/Heading' +import { List, ListItemGood } from '@/components/List' +import Link from 'next/link' + +The integer is stored in a variable using `System.in`, and is displayed on the screen using `System.out`. + +To understand this example, you should have the knowledge of the following Java programming topics: + +- [Java Hello World Program](/docs/hello-world) +- Java Basic Input and Output + +## 1. Print an Integer + +A Java program that prints a number predefined by the user is as follows: + +### Example: How to Print an Integer + +#### Input + +```java +public class HelloWorld { + + public static void main(String[] args) { + + // assing a variable to store the integer + int number = 10; + + // println() prints the following line to the output screen + System.out.println("The number is: " + number); + } +} + +``` + +#### Output + +```text n +The number is: 10 +``` +In this program we are printing a constant integer value which is definrd from first. +The integer is stored in an integer variable `number` using the keyword `int` and printed using the keyword `println`. + +## 2. Print an Integer (Entered by the User) + +A Java program that prints a number entered by the user is as follows: + +### Example: How to Print an Integer entered by an user + +#### Input + +```java +import java.util.Scanner; + +public class HelloWorld { + + public static void main(String[] args) { + + // Creates a reader instance which takes + // input from standard input - keyboard + Scanner reader = new Scanner(System.in); + System.out.print("Enter a number: "); + + // nextInt() reads the next integer from the keyboard + int number = reader.nextInt(); + + // println() prints the following line to the output screen + System.out.println("You entered: " + number); + } +} + +``` + +#### Output + +```text n +Enter a number: 10 +You entered: 10 +``` + +In this program, an object of `Scanner` class, `reader` is created to take inputs from standard input, which is `keyboard`. + +Then, `Enter a number` prompt is printed to give the user a visual cue as to what they should do next. + +`reader.nextInt()` then reads all entered integers from the keyboard unless it encounters a new line character `\n (Enter)`. The entered integers are then saved to the integer variable `number`. + +If you enter any character which is not an integer, the compiler will throw an `InputMismatchException`. + +Finally, `number` is printed onto the standard output `(System.out)` - computer screen using the function `println()`. \ No newline at end of file