Skip to main content

Posts

Showing posts with the label Java

Packages and Imports

Packages and Imports: Packages are directories with classes and methods, Import used to get the classes and methods from Inbuilt or created packages. Syntax:                                       import package_name; Here is the Code: //Package myPackage; (directory) // Java file in drive:Main.java import myPackage; class Main { public static void main (String[] args) { A a = new A(); a.getMe(); } } // Java file in drive:/myPackage/A.Java  package myPackage; public class A  {     public static void getMe()     {         System.out.println("Thank you");     } } /* compile:/myPackage> javac A.java  compile: javac Main.java  run: java Main  Output: Thank you */ Here is the Video:

Inheritance

Inheritance | Java: Inheritance means gets the properties from parent. With this child class can access the variables or methods from parent class. Syntax:                            class child_class extends parent_class . Here is the Code: import java.io.*; class Main { public static void main (String[] args) { A a = new A(1); B b = new B(3); System.out.println(a.getX()); System.out.println(b.getX()); } } class B extends A  {     public B(int x)     {         super(x);     } } class A  {     int x;     A(int x)     {         this.x = x;     }     public int getX()     {         return this.x;     } } Output: 1 3 Here is the Video:

Methods

Methods | Java: Methods are behavior of class.  Syntax:                                class class_name {                                                             void/int method_name(par1,par2,...,parn)                                                             {                                                                           // statements...                                                             }                                              } Here is the Code: import java.io.*; class Main { public static void main (String[] args) { A a = new A(); a.setX(3); System.out.println(a.getX()); } } class A  {     int x;     void setX(int x)     {         this.x = x;     }     int getX()     {         return this.x;     } } Output: 3 Here is the Video:

Access Modifiers

Access Modifiers | Java: Access Modifiers are controllers of data of variables, methods and classes. Those are four default, public, protected and private. Syntax:                                        access_madifier int x; Here is the Code: import java.io.*; class Main { public static void main (String[] args) { A a = new A(1); System.out.println(a.getX()); } } class A  {     private int x;     A(int x)     {         this.x = x;     }     public int getX()     {         return this.x;     } } Output: 1 Here is the Video:

Constructors

Constructors | Java: Constructor is called when the object is created. It is also a method with the same name of class. Syntax:                        class class_name {                                                 // statements.                                                 class_name(par1,par2,...,parn) // constructor                                                 {                                                              // statements                                                  }                                      } Here is the Code: import java.io.*; class Main { public static void main (String[] args) { A a = new A(1); A b = new A(); System.out.println(a.x); System.out.println(b.x); } } class A {     int x;     A(int x)     {         this.x = x;     }     A()     {         this.x = 2;     } } Output: 1 2 Here is the Video:

Classes and Objects

Classes and Objects | Java: Classes are custom/used data types, and Objects are instances of the data type. Syntax:           Class:         class_name {                                                      // statements...                                            }                         Oject:     class_name object_name = new class_name();  Here is the Code:  import java.io.*; class Main { public static void main (String[] args) { A a = new A(); a.x = 1; System.out.println(a.x); B b = new B(); b.y = 2; b.z = 3; System.out.println(b.y); a.c = b; System.out.println(a.c.z); } } class A {     int x;     B c; } class B {     int y;     int z; } Output: 1 2 3 Here is the Video:

Static methods

Static Methods | Java: Static methods are having collection of statements to perform particular task. Syntax:                              static void/int method_name(par1,par2,...,parn)                                           {                                                          // statements.                                            } Here is the Code: import java.io.*; class Main { public static void main (String[] args) { A(); B(); print("Hello"); sum(1,2); int a = mult(1, 2); System.out.println(a); } static void A() {     System.out.println("I am in A"); } static void B() {     System.out.println("I am in B"); } static void print(String str) {     System.out.println(str); } static void sum(int a,int b) {     System.out.println(a+b); } static int mult(int a,int b) {     return a * b; } } Output: I am in A I am in B Hello 3 2 Here is the Video:

Arrays

Arrays | Java: Arrays are using for storing the multiple same data type values. Syntax:                                      int[] a = {1, 2, 3};                                                   int[] b = new int[n];  where n is the size of an array. Here is the Code: import java.io.*; class Main { public static void main (String[] args) { int[] a = {1, 2, 3}; System.out.println(a[2]); for(int i=0;i<a.length;i++) {     System.out.println(a[i]); } int[] b = new int [5]; for(int i=0;i<b.length;i++) {     b[i]=5;     System.out.println(b[i]); } } } Output: 3 1 2 3 5 5 5 5 5 Here is the Video:

Conditional Statements: Loops

Java | Loops: Loops also conditional statements. There are two loops one is while loop and another one is for loop. Loops are executes the statements multiple times until the condition of loop become false. Syntax:                                while( condition )                                              {                                                          // statements...                                              }                                              for(variable assignment; condition ; modification of variable)                                             {                                                                // statements...                                             } Here is the Code: import java.io.*; class Main { public static void main (String[] args) {     int i = 1;     while(i <= 5)     {         System.out.print(i+" ");         i++;     }     System.out.println();     int sum = 0;     for(int j=1;j<=5;j++)

Conditional Statements: If - Else Statement

Java | If - Else Statement: If - else statement is one of the conditional statement. if checks the condition, if condition is true if block executes otherwise else block executes. Syntax:                   if ( condition )                                {                                        // statements...                                 }                                 else                                 {                                         // statements...                                  } Here is the Code: import java.io.*; class Main { public static void main (String[] args) {     int i = 1;     if( i % 2 == 0)     {         System.out.println("Even");     }     else     {         System.out.println("Odd");     }     int a = 1, b = 2;     int min;     if( a < b )     {         min = a;     }     else     {         min = b;     }     System.out.println("Min value is :" + min); } } Output: Odd Mi

Conditional Statements: If Statement

Java | If Statement: If statement is one of conditional statement, it execute the code in the if block only condition is true. Syntax :             if ( condition )                                 {                                 // statements...                            } Here is the Code: import java.io.*; class Main { public static void main (String[] args) {     int i = 1;     if( i % 2 != 0)     {         System.out.println("i is odd number.");     } } } Output: i is odd number. Here is the Video:

Conditional Statements: Switch Statement

Java | Switch Statement: Switch statement is the one of the conditional statement. It provide the multiple options to choose the one of them by checking the condition. Syntax:                           switch ( condition )                                        {                                                  case 1:                                                          // statements...                                                         break;                                                  case 2:                                                         // statements...                                                         break;                                                  default:                                                        // statements...                                         } Here is the Code: import java.io.*; class Main { public static void main (String[] args) {     int i = 3;     switch(i)     {         case 1:             System.out.

Operators

Java | Operators: Operators are used for performing calculations. List of Operators:          Additional Operator: Add two values with this additional operator, it is denoted by '+'.                                                      Ex: 100 +  1.         Subtraction Operator: Minus one value from another value with this operator, it is denoted by '-'.                                                       Ex: 100 - 1.         Multiplication Operator: Multiply two values with this operator, it is denoted by '*'.                                              Ex: 100 * 1.         Division Operator: Divide one value from another value with this operator, it is denoted by '/'.                                              Ex: 100 / 1.         Modulo Operator: Remainder of dividing one value with another value, it is denoted by '%'. Here is the Code: import java.io.*; class Main { public static void main (String[] args) {     // Addition     Syst

Relational operators

Java | Relational Operators: Relational operators are using for compare the data. List of Relational Operators: .     Less Than: This operator checks if left value is less than right value or not. it is denoted by '<'.                                      Ex: 1 < 2.     Greater Than: This operator checks if left value is greater than right value or not. it is denoted by '>'.                                      Ex: 1 > 2.       Equal to: This operator is using to check if both values are equal or not. it is denoted by '=='.                                     Ex: 1 == 2.       Not Equal to: This operator is using to check if both values are not equal or not. it is denoted by '!='.                                      Ex: 1 != 2.       Less Than or Equal to: This operator checks if left value is less than or equal with right value or not. it is denoted by '<='.                                             Ex: 1 <= 2.         Great

Variables

Java | Variables: Variables are using to store the data. Details of Variables:     Variable Declaration and Initialization: Create the variable and assign the value to it.                                   Ex: int i = 1.     Modification of Variables: Variables can modify by doing operators on them.                              Ex: i = i + 2.     Types of Variables: Variable types are integer, string, float/double and boolean.                                      Ex: 1 is integer, "Java" text with double quotes is string, 1.43 number with decimal values are float/double and true/false is boolean.  Here is the Code: import java.io.*; class Main { public static void main (String[] args) {     // Declaration and Assigning     int i = 1;     System.out.println(i);     // Modification     i = i + 2;     System.out.println(i);     // Types     double j = 1.43;     System.out.println(j);     String s = "Java";     System.out.println(s);     boolea

Data types

Java | Data Types: Java data types are strings, numbers and boolean values. One by One: Strings:          The strings are nothing but collection of characters and also text is in between double quotes.          Numbers:         The numbers are sub divided into int, float, double and long.         Ex: 10 is int, 3.14F is float, it takes up to 7 decimal positions, 2.414343 is double,            it takes up to 15 decimal positions and 1234565432112345L is long Boolean:          There are two boolean values, those are true and false. Here is the Code: import java.io.*; class Main { public static void main (String[] args) {      // String      System.out.println("cool");      // Integer  System.out.println(1); // float takes up to 7 decimal positions System.out.println(3.1421242323243554545F); // double takes up to 15 decimal positions System.out.println(2.4143245423242342321); // long  System.out.println(12345432123454321L); // boolean System.ou

Hello World

Java | Hello World: Most well know beginning word "Hello World" for any programming language.  Here is the Code: import java.io.*; class HelloWorld { public static void main (String[] args) { System.out.println("Hello World"); } }       Output: Hello World. Here is the Video:

Structure of Java Program

Java | Structure: The general structure of Java program, here you will learn to execute statement and fix the common syntax errors. Step by Steps: First create the class name as Main. Add main method (public static void main(String[] args), it is the beginning point of execution. Use println for print the statements. Most common syntax errors are missing semicolon and unbalanced brackets. Comments are created with // for code understanding purpose, comments are not executed. Here is the Code: import java.io.*; class Main { public static void main (String[] args) {     //Comments... System.out.println("Structure of Java."); } } Output: Structure of Java. Here is the Video:

Hello World

Program to print "Hello World" using C++. #include <iostream> using namespace std; int main() {           cout<<"Hello World";           return 0; } Here is the link for online execution: https://ide.geeksforgeeks.org/0pcL5B3xy5 . Program to print the "Hello World" using C. #include <stdio.h> int main() {          printf("Hello World");          return 0; } Here is the link for online execution: https://ide.geeksforgeeks.org/kA1mwOavW9  . Program to print "Hello World" using Java. import java.io.*; class HelloWorld {        public static void main(String args[])       {               System.out.println("Hello World");        } } Here is the link for online execution: https://ide.geeksforgeeks.org/hay0o092zV  .