You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lessons/io-conditionals.md
+10-8Lines changed: 10 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,16 +20,18 @@ Okay so let's learn about the control flow of a Java program.
20
20
21
21
Let's go through the code line by line.
22
22
23
-
-**``public static void main(String[] args)``** Java main method is the entry point of any java program. You can only change the name of String array argument, for example you can change ``args`` to ``myStringArgs``.
24
-
-**``public``** This is the access modifier of the main method.It has to be ``public`` so that java runtime can execute this method. Remember that if you make any method non-public then it’s not allowed to be executed by any program, there are some access restrictions applied. So it means that the main method has to be public.
25
-
-**``Static``** When java runtime starts, there is no object of the class present. That’s why the main method has to be static so that JVM can load the class into memory and call the main method. If the main method won’t be static, JVM would not be able to call it because there is no object of the class is present.
23
+
-**``public static void main(String[] args)``** Java main method is the entry point of any Java program. You can only change the name of String array argument, for example you can change ``args`` to ``myStringArgs``.
24
+
-**``public``** This is the access modifier of the main method.It has to be ``public`` so that Java Runtime Environment(JRE) can execute this method. Remember that if you make any method non-public then it’s not allowed to be executed by any program, there are some access restrictions applied. So it means that the main method has to be public.
25
+
-**``Static``** When JRE starts, there is no object of the class present. That’s why the main method has to be static so that JVM can load the class into memory and call the main method. If the main method won’t be static, JVM would not be able to call it because there is no object of the class is present.
26
26
-**``Void``** Java programming mandates that every method provide the return type. Java main method doesn’t return anything, that’s why it’s return type is void. This has been done to keep things simple because once the main method is finished executing, java program terminates. So there is no point in returning anything, there is nothing that can be done for the returned object by JVM. If we try to return something from the main method, it will give compilation error as an unexpected return value.
27
27
-**``Main``** method is similar to the main function in C and C++.
28
28
The ``main`` method accepts a single argument: an array of elements of type String.
29
29
-**``String[] args``** Java main method accepts a single argument of type String array. This is also called as java command line arguments.
30
30
-**``System.out.println("Hello, World");``** Outputs the string “Hello, World” followed by a new line on the screen.Output is actually accomplished by the built-in println( ) method. ``System`` is a predefined class that provides access to the system, and ``out`` is the variable of type output stream that is connected to the console.
31
31
32
-
Okay that's a lot of information to digest and it is the basic for a C++ program. Now let's move on to the next section.
32
+
> System, out, println these are default classes and methods built into Java. If you want to read docs do a `ctrl+click` (IntelliJ Idea) on them.
33
+
>
34
+
Okay that's a lot of information to digest and it is the basic for a Java program. Now let's move on to the next section.
33
35
34
36
## **If-Else Statements**
35
37
@@ -41,7 +43,7 @@ It will be really easy to understand as we will see in the following example.
41
43
42
44
if-else statements are written as follows.
43
45
44
-
```cpp
46
+
```java
45
47
// syntax
46
48
if (condition) {
47
49
// Code to execute if condition is true
@@ -85,9 +87,9 @@ ok so that's all. now let's see some more examples.
85
87
publicclassIfElseClass {
86
88
publicstaticvoidmain(String[] args) {
87
89
//defining a variable
88
-
int number=13;
90
+
int number=13;
89
91
//Check if the number is divisible by 2 or not
90
-
if(number%2==0){
92
+
if(number%2==0){
91
93
System.out.println("even number");
92
94
}
93
95
else{
@@ -109,7 +111,7 @@ Switch statements are used to execute different code depending on the value of a
0 commit comments