Search This Blog

Showing posts with label Java 8 - Book. Show all posts
Showing posts with label Java 8 - Book. Show all posts

Wednesday, July 20, 2016

Another way to convert an int to String

import java.util.Scanner;
public class IntToStrMethodsDemo {
    public static void main(String[] args) {
        int num;
        num=readInt();
        String str=intToStr(num);
        System.out.println(intToStr(num)+1);
    }
    public static int readInt(){
        System.out.println("enter an int");
        return new Scanner(System.in).nextInt();
    }
    public static String intToStr(int num){
        return ""+num;
    }

}
Or use the code below :

String str = ""+new Scanner(System.in).nextInt();
        System.out.println(str+1);


It does the same

Saturday, July 16, 2016

Simple utility to convert an int to String

package utilities;
import java.util.Scanner;
public class IntToStr {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter an integer :");
        int num = scanner.nextInt();
        String str = intToStr(num);
        System.out.println("number String : "+str);
    }
    public static String intToStr(Integer num){
        return num.toString();
    }

}

Friday, December 18, 2015

Enhanced for loop and Immutability?

There I find a twist in the enhanced for loop and immutable nature. Where it does not change the data in an array of primitives, here I can see a clear variation.
Here is a Person class.
package test;
public class Person {
      private int pId;
      private String name;
      public Person(int pId, String name) {
          this.pId = pId;
          this.name = name;
     }
    public String toString(){
            return "Id : "+pId+" Name : "+name;
    }
    public int getpId() {
           return pId;
    }
    public void setpId(int pId) {
         this.pId = pId;
   }
   public String getName() {
         return name;
   }
   public void setName(String name) {
        this.name = name;
    }
}
And here is a tester class with enhanced for loop.
package test;
import static java.lang.Math.random;
import java.util.ArrayList;
import java.util.List;
public class PersonList {
      public static void main(String[] args) {
           List<Person> pList = new ArrayList<>();
           pList.add( new Person(101,"John"));
           pList.add( new Person(102,"James"));
           pList.add( new Person(103,"Dan"));
           pList.add( new Person(104,"Mathew"));
           pList.add( new Person(105,"Sam"));

          System.out.println("Added List elements : "+pList);
         /* To perfomr element wise operation */
          for(Person p : pList){
                System.out.println(p);
          }
         /* you try to modify the data in pList through enhanced for loop */
                System.out.println("List elements now : ");
                for(Person p : pList){
                     if(p.getName().equals("Sam"))
                          p.setName("Samuel");
                 }
/* OOPs... it has not changed...But let us conform it once again by using enhanced for loop */
          System.out.println("After Updation : view element in the list : ");
          for(Person p : pList){
                 System.out.println(p);
          }
  }
}
When I run this I see something different.
Added List elements : [Id : 101 Name : John, Id : 102 Name : James, Id : 103 Name : Dan, Id : 104 Name : Mathew, Id : 105 Name : Sam]
Id : 101 Name : John
Id : 102 Name : James
Id : 103 Name : Dan
Id : 104 Name : Mathew
Id : 105 Name : Sam
List elements now : 
After Updation : view element in the list : 
Id : 101 Name : John
Id : 102 Name : James
Id : 103 Name : Dan
Id : 104 Name : Mathew
Id : 105 Name : Samuel
Now the question is why the name gets changed from within the enhanced for loop. Now in previous post x reads data from ArrayList, and value change in x does not reflect in ArrayList. But in current example, p holds the reference to an object and using reference, it change the value. Rest is left to your imagination... :) !!!


Friday, June 19, 2015

Interesting facts about JAVA - 1

Did you know that the first name given to Java was OAK. And it is NOT an acronym. The name OAK was inspired by the  Oak tree that was seen from the office window.