Posts

Showing posts from October, 2022

What does System.out.printf( "%-15s%03d\n", s1, x) do?

What does System.out.printf( "%-15s%03d\n", s1, x) do?   First   s1   which is formatted using   %-15s . The   %   means that what follows is an argument that will be formatted. Then follows a   -   resulting in left alignment.   15   fills the string up to a length of 15 characters (adding spaces at the end). Finally the   s   means, that you are formatting a string. Second  x  which is formatted using  %03d . Here the  0  is the fill character, meaning that, if necessary, zeros are added. The  3  is again the width, meaning the fill character  0  is added as many times as necessary to make it 3 digits long (this time at the beginning). Finally  d  means, that a integer is formatted. OR "%-15s" means that within 15 blank space, the String "s1" will be filled in the left. (fill in the blanks from the left) "%03d" means that within 3 0s, the integer"x" will be filled in the right.(fill in the zeros from the right). This can only be done

Day24 : Argument Passing

Argument Passing  In Java we can pass two types of arguments to a method and they are Variables (Primitive Datatypes) References (Array References & Object Reference Passing Variable Whenever we pass any variable as argument to a method then java passes its value. This value is received  by the formal argument declared in the method's argument list. So now both the argument, actual and formal have the same value but their address are different so if we make any change in the value of formal argument then it will never effect the value of actual argument. Thus we say that in Java variables are always passed using pass by value. The Second type of argument which we can pass to any method in Java is reference (Array Reference and Object Reference). Whenever we pass any reference as argument to a method then Java passes the addresses of the object or array pointed by the reference. This address is then received by formal reference declared in the argument list of the method. so now

Day23 : Method Overloading

 Method Overloading The word overloading in object oriented world means multiple version of the same thing. Thus the word Method overloading means providing multiple method in the same scope with the same name. For Example :  The two popular method in java called println and print have multiple version and thus are overloaded.          (1) int a = 10;                  System.out.println(a);          (2) double b = 1.7;                 System.out.println(b);          (3) String name = "Sachin";                  System.out.println("Sachin"); But if we're overloading two or more method then these method must compulsory differ from each other in terms of their argument and this difference can be of three types: Number of Argument can be different. Datatype of Argument can be different. Order of Argument can be different. We must remember that overloading is never possible just on the basis of return type i.e., if two method are having same name and same argument the

Day22 : Object Initialization

Image
 Object Initialization The word object initialization in Java means assigning initial value to instance variable at the time of creation of the object. In Java we've three ways of doing this Using Explicit initialization Using Constructor Using Initializer block 1 - Using Explicit Initialization          class < class-name>          {               <array> <data_type> <var> = value(This is an explicit initialization);          } Example : File Name : Account.java public class Account { private int accId = 101 ; private String custName = "Amit" ; private double balance = 50000.0 ; public void showAccount () { System . out . println ( "accId " + accId ); System . out . println ( "Customer Name " + custName ); System . out . println ( "Balance " + balance ); } } File Name : UseAccount.java public class UseAccount { public static void main ( String [] args ) { Acc

Day21: Object Oriented Programming System(OOPs)

Object Oriented Programming System(OOPs)  Object Oriented Programming is not a programming Language. Rather it is a methodology approach to design programs which are close to read world and help us visualize real world situation in our software. The biggest advantage of Object oriented programming- Data Security Easy Modeling of Real world situation What are the two basic building block of OOPs? The most important component of object oriented programming are: Classes Objects Class: A class is just like a blueprint Or Template which describes how an object will look. For Example :      Model of building, Develop by civil engineer is like a class while actual building built up by the Labourers is object. In Simple terms we can say classes is logical while object are physical. What does a class contain? A class contain two important element. (1) Instance Variable -          They are also called data member in languages like C++ and they hold the actual data stored in an object. (2) Method

Day 20 : Double Dimensional Array

Image
 Double Dimensional Array In Java 2D Array are used in those situations where we have to stored multiple groups of data . For Example :      Suppose we want to record marks of three student, where each student has five subject. In this case we have two solution - We can declare Three 1D array each of size 5 But the code will become difficult because we will have to handle 3 Arrays individually.  A better solution would be to take 2D array of three rows and five column. The benefit will be we will have to handle 1 array which can be handle using only 1 Loop. Type of 2D Array In Java we have two types of 2D Array. Rectangular  2D Array Jagged 2D Array Rectangular 2D Array These are those 2D array where every row contains same number of columns i.e., Length of every row contains same number of column. See Fig1. Jagged 2D Array These are those 2D Array where each rows can be of different length. See Fig2. Creating a Rectangular 2D Array 1. Creating the Array Reference <data_type> [

Day19 : Using for Each Loop

Image
 Using For-Each Loop Syntax :                for <data_type><var> : <array_name>               {                    //Statement               } int [ ] arr = {10,20,30,40,50}                                   for(int x : arr)                    {                         System.out.println(x);                    }      Output :                10 20 30 40 50  Advantages of for-each loop: Syntax is easy compared to regular for loop because initialization, condition, increment is automatically done by java. No chances of Loop running infinitely. No chances array index out of Bounds Exception Drawback Or Restriction It will always traversed the array from index[0] to length[-1] It will pass through every element of the array. i.e., we cannot skip any element It can only access array data but cannot make any kind of change in it. We should use this loop for full array traversal and when we don't want to make any changes in array values. Problem : Write a Java program to

Day18: How java handles deallocation of dynamic block

Image
 How java handle deallocation of dynamic block Before we can understand how java handles deallocation of dynamic block, we first have to understand terminology called as :  Garbage Block Garbage Collector Garbage block in java are those are not pointed by or referred by any reference. If an object reference or an array reference stop pointing to its object and array garbage block. int [ ] arr = new int[]; Now inside JVM there is a  special software called as garbage collector which scans heap area time to time, identified garbage block and then automatically destroy them. As mention above the GC remains under the control of JVM and programmer has no control no role in this process. Then we say that in java Allocation of dynamic block is done as programmer request. But the deallocation is automatically done by the JVM and so we also say Java have automatic memory management. Initializing an Array We can initialize an array in three format: (1) int [ ] months = new int[12];          

Day17: Arrays

Image
 Arrays In Java just like C and C++ arrays are collection of similar types of Data element stored at continuous memory locations. But there are few more properties associated with arrays and they are -  In Java Arrays are Object Since Array are object they are always created dynamically using the keyword "new". Since arrays are object they always live in heap area in memory. Since arrays are object we require an array reference to access them . Creating an Array : 1 - Creating array reference  <data_type>[ ] <array ref>; OR <data_type><array_ref>[ ]; 2- Creating the Actual Array <array_ref> = new<datatype>[size]; int [ ] arr; (Read as : arr is a reference to an integer array) arr = new int[10]; OR int[] arr = new int[10]; Accessing the Array: class ArrayDemo { public static void main(String [ ] args) { int [ ] arr = new int[5]; arr[0] = 15; System.out.println(arr[0]); arr[1] = 20; System.out.println(arr[1]); arr[5] = 5

Day16: Nested Loop

Image
 Nested Loop In the nested loop, loop are going inside loop whose syntax are given below: for(initializer; test_cond; increment or decrement) {      for(initializer; test_cond; increment or decrement)          {               // loop body          }    //loop body } Example :  class Nestloop { public static void main(String [ ] args) { for(int i = 1; i<=3; i++) { for(int j = 10; j<=15; j++) { System.out.print(j+" "); } System.out.println(); } } } Labelled Break; class Nestloop { public static void main(String [ ] args) { bhopal: for(int i = 1; i<=3; i++) { for(int j = 10; j<=15; j++) { System.out.print(j+" "); if(j==14) { break bhopal; } } System.out.println(); } System.out.println("Thank You"); } } Exit: class Nestloop { public static void main(String [ ] args) { bhopal: for(int i = 1; i<=3; i++) { for(int j = 10; j<=15;