Java Programming language Basics Concepts
Tags: java, beginners, tutorial, codenewbie
Published: 2020-08-19 07:06:38

<!-- wp:heading -->
<h2></h2>
<!-- /wp:heading -->

<!-- wp:heading {"level":4} -->
<h4>Java is a very popular programming language. Java can be used to develop anything and almost everything like web applications, web servers, application servers, mobile applications and so on</h4>
<!-- /wp:heading -->

<!-- wp:heading -->
<h2>Basics</h2>
<!-- /wp:heading -->

<!-- wp:heading {"level":3} -->
<h3>Sample Java program</h3>
<!-- /wp:heading -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">import java.util.Date;

public class HelloWorld {
    public static void main(String[] args) {
        Date now = new Date();
        System.out.println("Hello World!");
        System.out.println("now: " + now);
    }
}</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:list -->
<ul><li>import&nbsp;- used to import java libraries</li><li>class keyword&nbsp;- to declare a class in java.</li><li>public keyword&nbsp;- access modifier represents visibility. Public is visible to all.</li><li>static keyword&nbsp;- no need to create an object to invoke the static method. There is no need to create an object to invoke the main method and it's invoked by JVM and saves memory.</li><li>void&nbsp;- return type of the method and it doesn't return any value.</li><li>main&nbsp;- starting point of the program.</li><li>String[] args&nbsp;- for command line arguments</li><li>System.out.println()&nbsp;- used to print statement.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>To Compile a Java program</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Go to Command prompt and navigate to the folder where java files are stored.</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">javac example.java</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:heading {"level":3} -->
<h3>To Run Java program</h3>
<!-- /wp:heading -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">java example </pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:heading {"level":3} -->
<h3>Comments</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><code>//</code>&nbsp;- single line comment</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Data types</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Two groups of data types</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>1. Primitive data types</h3>
<!-- /wp:heading -->

<!-- wp:table -->
<figure class="wp-block-table"><table><thead><tr><th>Data type</th><th>Description</th><th>Range</th><th>Size</th></tr></thead><tbody><tr><td>int</td><td>used to store whole numbers</td><td>-2,147,483,648 to 2,147,483,647</td><td>4 bytes</td></tr><tr><td>short</td><td>used to store whole numbers</td><td>-32,768 to 32,767</td><td>2 bytes</td></tr><tr><td>long</td><td>used to store whole numbers</td><td>-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807</td><td>8 bytes</td></tr><tr><td>byte</td><td>used to store whole numbers</td><td>-128 to 127</td><td>1 byte</td></tr><tr><td>float</td><td>used to store fractional numbers</td><td>6 to 7 decimal digits</td><td>4 bytes</td></tr><tr><td>double</td><td>used to store fractional numbers</td><td>15 decimal digits</td><td>8 bytes</td></tr><tr><td>boolean</td><td>can either store true or false</td><td>either true or false</td><td>1 bit</td></tr><tr><td>char</td><td>used to store a single character</td><td>one character</td><td>2 bytes</td></tr></tbody></table></figure>
<!-- /wp:table -->

<!-- wp:heading {"level":3} -->
<h3>Examples</h3>
<!-- /wp:heading -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">int x = 99999; 
short x = 999; 
long x = 99999999999L;
byte x = 99;
double x = 99.99d;
boolean isAvailable = true;
char division = 'A';</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:heading {"level":3} -->
<h3>2. Non-Primitive data types</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Non-primitive data types specifies the complex data values. For example, strings, arrays and classes can be referred as Non-primitive data types.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Variables</h2>
<!-- /wp:heading -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">data-type variable-name = value;</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:heading {"level":3} -->
<h3>Naming convention of variables</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Case sensitive.</li><li>Must should begin with a&nbsp;<code>lower case letter</code>. For second word should be a capital like this: firstName, pinCode etc.</li><li>Cannot contain white spaces like first name.</li><li>special characters like&nbsp;<code>$</code>&nbsp;and&nbsp;<code>_</code>&nbsp;can be used to begin a variable name.</li></ul>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Types of Variables</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Local Variables</li><li>Instance Variables</li><li>Static Variables</li></ul>
<!-- /wp:list -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">Class Sum {
    int n1 = 10; // value of Instance  Variables are are instance specific.
    static int n2 = 20; //static variable is Common for all the instances of the class.
    void sum(){
        int n3 = 30; //local variable and they are Inside the body of a method.
        int total = n1+n2+n3;
    }
}</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:heading -->
<h2>Data Conversions</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>String to Number<code>int i = Intege­r.p­ars­eIn­t(­str); double d = Double.pa­rse­Dou­ble­(s­tr);</code></li><li>Other types to String<code>String str = String.va­lue­Of(­va­l);</code></li></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2>Operators</h2>
<!-- /wp:heading -->

<!-- wp:table -->
<figure class="wp-block-table"><table><thead><tr><th>Operator type</th><th>Description</th></tr></thead><tbody><tr><td>Arithmetic Operator</td><td>+ , - , * , / , %</td></tr><tr><td>comparision Operator</td><td>&lt; , &gt; , &lt;= , &gt;=, != , ==</td></tr><tr><td>Bitwise Operator</td><td>&amp; , ^ ,&nbsp;|</td></tr><tr><td>Logical Operator</td><td>&amp;&amp; ,&nbsp;<code>||</code>, !</td></tr><tr><td>Assignment Operator</td><td>= , += , -= , *= , /= , %=</td></tr><tr><td>Auto-increment and Auto-decrement Operators</td><td>++ , --</td></tr><tr><td>Ternary Operator</td><td>? :</td></tr></tbody></table></figure>
<!-- /wp:table -->

<!-- wp:heading -->
<h2>Arrays</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Array is a collection of similar data which is stored in continuous memory addresses.</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">data-type[] array-name; // one dimensional array
data-type[][] array-name; // two dimensional array</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:heading {"level":3} -->
<h3>Example</h3>
<!-- /wp:heading -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">String[] mobiles = {"iPhone", "Samsung", "OnePlus"};
for (int i = 0; i &lt; mobiles.length; i++) {
  System.out.println(mobiles[i]);
}
mobiles[1] = "Oppo"; // change an array element</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:heading -->
<h2>Conditional Statements</h2>
<!-- /wp:heading -->

<!-- wp:heading {"level":3} -->
<h3>1. If</h3>
<!-- /wp:heading -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">if(conditional-expression)
{
    //code
}</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:heading {"level":3} -->
<h3>2. If-else</h3>
<!-- /wp:heading -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">if(conditional-expression)
{
    //code
} else {
    //code
}</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:heading {"level":3} -->
<h3>3. If-else-if ladder</h3>
<!-- /wp:heading -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">if(conditional-expression-1)
{
    //code
} else if(conditional-expression-2) {
    //code
} else if(conditional-expression-3) {
    //code
}
....
else {
    //code
}</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:heading {"level":3} -->
<h3>4. Nested-If</h3>
<!-- /wp:heading -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">if(conditional-expression-1) {    
     //code    
          if(conditional-expression-2) {  
             //code
             if(conditional-expression-3) {
                 //code
             }  
    }    
}</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:heading {"level":3} -->
<h3>5. Switch</h3>
<!-- /wp:heading -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">switch(conditional-expression){    
case value1:    
 //code    
 break;  //optional  
case value2:    
 //code    
 break;  //optional  
...    
    
default:     
 //code to be executed when all the above cases are not matched;    
} </pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:heading -->
<h2>Loops</h2>
<!-- /wp:heading -->

<!-- wp:heading {"level":3} -->
<h3>1. For</h3>
<!-- /wp:heading -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">for(Initialization; Condition; Increment/decrement){  
//code  
} </pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:heading {"level":3} -->
<h3>2. While</h3>
<!-- /wp:heading -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">while(condition){  
//code 
}  </pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:heading {"level":3} -->
<h3>3. Do-While</h3>
<!-- /wp:heading -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">do{  
//code 
}while(condition); </pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:heading -->
<h2>Collections</h2>
<!-- /wp:heading -->

<!-- wp:table -->
<figure class="wp-block-table"><table><thead><tr><th>Collection</th><th>Description</th></tr></thead><tbody><tr><td>Set</td><td>Set is a collection of elements which can not contain duplicate values. Set is implemented in HashSets, LinkedHashSets, TreeSet etc</td></tr><tr><td>List</td><td>List is a ordered collection of elements which can have duplicates. Lists are classified into ArrayList, LinkedList, Vectors</td></tr><tr><td>Queue</td><td>FIFO approach, while instantiating Queue interface you can either choose LinkedList or PriorityQueue.</td></tr><tr><td>Deque</td><td>Deque(Double Ended Queue) is used to add or remove elements from both the ends of the Queue(both head and tail)</td></tr><tr><td>Map</td><td>Map contains key-values pairs which don't have any duplicates. Map is implemented in HashMap, TreeMap etc.</td></tr></tbody></table></figure>
<!-- /wp:table -->

<!-- wp:heading -->
<h2>String Methods</h2>
<!-- /wp:heading -->

<!-- wp:table -->
<figure class="wp-block-table"><table><thead><tr><th>Method</th><th>Description</th><th>Example</th></tr></thead><tbody><tr><td>char charAt(int index)</td><td>returns char value at the specific index</td><td>str.charAt(0) //prints o</td></tr><tr><td>int compareTo(String str)</td><td>to compare two strings lexicographically</td><td>Str1.compareTo(Str2) //returns 0 if str1 and str2 are equal</td></tr><tr><td>int length()</td><td>returns string length</td><td>str.length()</td></tr><tr><td>static String format(String format, Object... args)</td><td>returns a formatted string.</td><td>String.format("String is %s",str);</td></tr><tr><td>String substring(int beginIndex, int endIndex)</td><td>to return substring from given begin index to end index.</td><td>str.substring(0,3) //prints one</td></tr><tr><td>String substring(int beginIndex)</td><td>to return substring from given begin index</td><td>str.substring(3) //prints compiler</td></tr><tr><td>boolean contains(CharSequence s)</td><td>returns true or false after matching the sequence given in the string</td><td>str.contains("compiler") // returns true</td></tr><tr><td>static String join(CharSequence delimiter, CharSequence... elements)</td><td>returns a joined string.</td><td>String.join("..","Hello","Happy", "Learning"); //returns Hello..Happy..Learning</td></tr><tr><td>boolean equals(Object another)</td><td>checks the equality of string with another and returns true if they are equal.</td><td>str1.equals(str2);</td></tr><tr><td>boolean isEmpty()</td><td>to check if the given string is empty.</td><td>str.isEmpty() // returns false</td></tr><tr><td>String concat(String str)</td><td>concatenates the provided string with the another string.</td><td>str.concat(" is used to compile code online")</td></tr><tr><td>String replace(char old, char new)</td><td>replaces all occurrences of the specified char value with new char value.</td><td>str.replace('r','t');</td></tr><tr><td>String replace(CharSequence old, CharSequence new)</td><td>replaces all occurrences of the specific CharSequence with new one.</td><td>str.replace('one','Online');</td></tr><tr><td>static String equalsIgnoreCase(String another)</td><td>compares another string with out considering case.</td><td>str1.equalsIgnoreCase(str2)</td></tr><tr><td>String[] split(String regex, int limit)</td><td>returns a split string matching regex and limit. here limit is optional</td><td>str.split("\s")//splits string based on whitespaces</td></tr><tr><td>int indexOf(String substring, int fromIndex)</td><td>returns the specified substring index starting with given index. here index is optional</td><td>str.indexOf("compiler",2);</td></tr><tr><td>String toLowerCase()</td><td>returns a string in lowercase.</td><td>str.toLowercase();</td></tr><tr><td>String toUpperCase()</td><td>returns a string in uppercase.</td><td>str.toUpperCase();</td></tr><tr><td>String trim()</td><td>removes beginning and trailing spaces of a given string.</td><td>str.trim();</td></tr></tbody></table></figure>
<!-- /wp:table -->

<!-- wp:heading -->
<h2>OOPS</h2>
<!-- /wp:heading -->

<!-- wp:heading {"level":3} -->
<h3>1. Class</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Class is the blueprint of an object and&nbsp;<code>class</code>&nbsp;keyword is required to create a class.</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">class class_name {  
    fields;  
    methods;  
}</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:heading {"level":3} -->
<h3>2. Object</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Object is a basic unit in OOP, and is an instance of the class.</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">class_name obj_name;</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:heading {"level":3} -->
<h3>3. Abstraction</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Data abstraction is a technique which provides only the required data to be visible or accessible to outside world.&nbsp;<code>abstract</code>&nbsp;keyword is used for classes and methods.</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">abstract class Mobiles { // abstract class
  public abstract void features();   // abstract method
  public void method-name() {
    //code
  }
}</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:heading {"level":3} -->
<h3>4. Encapsulation</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Encapsulation is a mechanism to protect private hidden from other users. It wraps the data and methods as a single bundle.&nbsp;<code>private</code>&nbsp;is the keyword used to declare the variables or methods as private. You can make public&nbsp;<code>set</code>&nbsp;and&nbsp;<code>get</code>&nbsp;methods to access private variables.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>5. Polymorphism</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Polymorphism gives the meaning many forms, usually it occurs when multiple classes are present and have been inherited.</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code -->
<pre class="wp-block-syntaxhighlighter-code">class Child-class extends Parent-Class {
//code
}</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->