Monday, June 27, 2016

number keypad for Input text in mobiles

by using usage property we can show only number pad to end user.

<af:inputText label="Label 1" id="it1" usage="#{'tel'}"/>

enum :java: basics

Enums were introduced in java 5.0. Enums restrict a variable to have one of only a few predefined values. The values in this enumerated list are called enums.
With the use of enums it is possible to reduce the number of bugs in your code.
For example, if we consider an application for a fresh juice shop, it would be possible to restrict the glass size to small, medium and large. This would make sure that it would not allow anyone to order any size other than the small, medium or large.

Example:

class FreshJuice {

   enum FreshJuiceSize{ SMALL, MEDIUM, LARGE }
   FreshJuiceSize size;
}

public class FreshJuiceTest {

   public static void main(String args[]){
      FreshJuice juice = new FreshJuice();
      juice.size = FreshJuice.FreshJuiceSize.MEDIUM ;
      System.out.println("Size: " + juice.size);
   }
}
Note: enums can be declared as their own or inside a class. Methods, variables, constructors can be defined inside enums as well.

Basics Java and ADF

set CLASSPATH=%CLASSPATH%;C:\Program Files\Java\JDK1.6.20\lib
set PATH=%PATH%;C:\Program Files\Java\JDK1.6.20\bin
1)The main difference between PATH and CLASSPATH is that  PATH is an environment variable which is used to locate JDK binaries like "java" or "javac" command used to run java program and compile java source file. On the other hand, CLASSPATH, an environment variable is used by System or Application ClassLoader to locate and load compile Java bytecodes stored in the .class file.

Another significant difference between PATH and CLASSPATH is that PATH can not be overridden by any Java settings but CLASSPATH can be overridden by providing command line option -classpath or -cp to both "java" and "javac" commands or by using Class-Path attribute in Manifest file inside JAR archive.



Java Identifiers:

All Java components require names. Names used for classes, variables and methods are called identifiers.
In Java, there are several points to remember about identifiers. They are as follows:

  • All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).
  • After the first character identifiers can have any combination of characters.
  • A key word cannot be used as an identifier.
  • Most importantly identifiers are case sensitive.
  • Examples of legal identifiers: age, $salary, _value, __1_value
  • Examples of illegal identifiers: 123abc, -salary
  • Access Modifiers: default, public , protected, private
  • Non-access Modifiers: final, abstract, strictfp


WhyBackingBean/mangedBean doesnot have main();
The main method is only used to start a program. A managed bean is never used to start a program. Such beans are used by web application which have another start class, having a main method.
You can add a main method to a managed bean, but this method will never be called by a running web application. As a managed bean contains attributes and sometimes logic which can' be used outside a web application (they have to run in a context which you can't setup from the main method) there is no need to have a main method in a managed bean

ADF : Get Current Logged User Name

when we wnable adf Security for application then SecurityContext will be created in backend,also creates jazan.data.xml and jps-config.xml file(Contains Security context)

Sometime we need getting current authenticated user name in ADF application.
To get user name we can do it using three ways

1- Using Groovy 
We can set default expression in view object attribute as below
adf.context.securityContext.getUserPrincipal().getName() 
or
adf.context.securityContext.getUserName()  

2- Java Code
You can get User Name in Java code also using the following code.

     ADFContext adfCtx = ADFContext.getCurrent();  
     SecurityContext secCntx = adfCtx.getSecurityContext();  
     String user = secCntx.getUserPrincipal().getName();  
     String _user = secCntx.getUserName();  

3-Expression Lnagugae
You can bind ADF Faces componenets with the following EL to get logged User Name.

 #{securityContext.userName}  

To displayed the logged-in user in the AMX page –
<amx:outputText value=” #{securityContext.userName}” id=”ot1″ />

To get the logged-in user in the Managed Bean –
String username = (String)AdfmfJavaUtilities.evaluateELExpression(“#{securityContext.userName}”);

Thursday, June 23, 2016

Tricky Codes :) Java

String newLine = System.getProperty("line.separator");
above code will add new line("\n")

while(true){System.out.println("hi Nani");}// Infinity Loop
// for(;;) // infinity for loop

Wednesday, June 22, 2016

Console : IO

import java.io.Console;

public class ConsoleDemo {
public static void main(String args[]){
Console c=System.console();
System.out.println("Enter password: ");
char[] ch=c.readPassword();
    c.readLine();
String pass=String.valueOf(ch);//converting char array into string
System.out.println("Password is: "+pass);
}
}  

Scanner class : Utility class

                Scanner sc = new Scanner(System.in);
System.out.println("Enter Name : ");

String name = sc.nextLine();
System.out.println("Enter Id : ");

int id = sc.nextInt();
System.out.println("enter salary per day : ");

double d = sc.nextDouble();

               // to show 0 decimals in double value after decimal floating-point values don't have decimal                           //digits. They have binary digits.
BigDecimal b = new BigDecimal(Double.toString(d));
b = b.setScale(2, BigDecimal.ROUND_HALF_UP);

System.out.println("Details :"+name+"\n"+id+"\n"+b);

Scanner class : Utility class

                Scanner sc = new Scanner(System.in);
System.out.println("Enter Name : ");

String name = sc.nextLine();
System.out.println("Enter Id : ");

int id = sc.nextInt();
System.out.println("enter salary per day : ");

double d = sc.nextDouble();

               // to show 0 decimals in double value after decimal floating-point values don't have decimal                           //digits. They have binary digits.
BigDecimal b = new BigDecimal(Double.toString(d));
b = b.setScale(2, BigDecimal.ROUND_HALF_UP);

System.out.println("Details :"+name+"\n"+id+"\n"+b);

Scanner class : Utility class

                Scanner sc = new Scanner(System.in);
System.out.println("Enter Name : ");

String name = sc.nextLine();
System.out.println("Enter Id : ");

int id = sc.nextInt();
System.out.println("enter salary per day : ");

double d = sc.nextDouble();

               // to show 0 decimals in double value after decimal floating-point values don't have decimal                           //digits. They have binary digits.
BigDecimal b = new BigDecimal(Double.toString(d));
b = b.setScale(2, BigDecimal.ROUND_HALF_UP);

System.out.println("Details :"+name+"\n"+id+"\n"+b);

Scanner class : Utility class

                Scanner sc = new Scanner(System.in);
System.out.println("Enter Name : ");

String name = sc.nextLine();
System.out.println("Enter Id : ");

int id = sc.nextInt();
System.out.println("enter salary per day : ");

double d = sc.nextDouble();

               // to show 0 decimals in double value after decimal floating-point values don't have decimal                           //digits. They have binary digits.
BigDecimal b = new BigDecimal(Double.toString(d));
b = b.setScale(2, BigDecimal.ROUND_HALF_UP);

System.out.println("Details :"+name+"\n"+id+"\n"+b);

Scanner class : Utility class

                Scanner sc = new Scanner(System.in);
System.out.println("Enter Name : ");

String name = sc.nextLine();
System.out.println("Enter Id : ");

int id = sc.nextInt();
System.out.println("enter salary per day : ");

double d = sc.nextDouble();

               // to show 0 decimals in double value after decimal floating-point values don't have decimal                           //digits. They have binary digits.
BigDecimal b = new BigDecimal(Double.toString(d));
b = b.setScale(2, BigDecimal.ROUND_HALF_UP);

System.out.println("Details :"+name+"\n"+id+"\n"+b);

DateFormat dd/MM/yyyy

                String pattern = "dd/MM/yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);

--------------------------------------------or else -----------------------------------------------------
                 java.util.Date date=new java.util.Date();
String pattern = "dd/MM/yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date1 = simpleDateFormat.format(date);
System.out.println(date1);  

Conversions All in one

To String :
                 int i = 123;
char []c = {'h','b','c'};
String s = String.valueOf(c);
String s1 = String.valueOf(i);
like this we can covert (double,boolen,float,long,object)(char[] data,int offset,int count);
int offset from which index it has to start and count indicates length
--------------------------------------------------------------------------------------------------------


String to int

String s = "10";
      int x =Integer.parseInt(s);
      int b = Integer.parseInt("444",16);
here 16 is radix which converts string into integer type
-------------------------------------------------------------------------------------------------------
integer to long

integer to char

           char c=(char)i;
String to Double


      double d = Double.parseDouble("20");

read file and store it into PropertiesObject in Java

package colections;

import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;

public class ReadNdStoreProps {
public static void main(String[] args) throws IOException, SQLException {
Properties p = new Properties();
//by using fis or fr we can read file and store it into properties using load and then we can use them across the bean
// FileInputStream fis = new FileInputStream("R:\\Bore\\Eclipse Workspace\\Testing\\src\\colections\\login.txt");
FileReader fr = new FileReader("R:\\Bore\\Eclipse Workspace\\Testing\\src\\colections\\login.txt");
p.load(fr);

String url = p.getProperty("url");
String user = p.getProperty("user");
String pswd = p.getProperty("pswd");
//by using newline or "\n" we can separate lines
String newLine = System.getProperty("line.separator");
System.out.println(url+newLine+user+" \n"+pswd);
fr.close();

}

}

FileWriter and FileReader

package colections;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FilereaderDemo {

public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("R:\\Bore\\Eclipse Workspace\\Testing\\src\\colections\\test1.txt");
String s = "Hello Raviteja welcome to India";
fw.write(s);

fw.close();

FileReader fr = new FileReader("R:\\Bore\\Eclipse Workspace\\Testing\\src\\colections\\test1.txt");
char []c = new char[500];
fr.read(c);
System.out.println(c);
//converting char to String
String s3 = String.valueOf(c);
System.out.println(s3);
//without converting char to string ,Using for loop
for(char s1 :c)
System.out.print(s1);
fr.close();


}

}

FileOutputStream and FileInputStream from IO Package

package colections;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class FileOutputDemo {

public static void main(String[] args) throws IOException {

FileOutputStream fos = null;
     FileInputStream fis = null;
     byte[] b = {25,47,30,25,24,28};
     int i=0;
     try{
        // create new file output stream - this one used to write stream of raw bytes such as image data to a file
        fos=new FileOutputStream("R:\\Bore\\Eclipse Workspace\\Testing\\src\\colections\\test.txt");
       
        // writes bytes to the output stream
        fos.write(b);
        // flushes the content to the underlying stream
        fos.flush();
        // create new file input stream -used to read stream of raw bytes from a file *-1 means reached end of the file*
        fis = new FileInputStream("R:\\Bore\\Eclipse Workspace\\Testing\\src\\colections\\test.txt");
        //skip(long n) : n defines no of bytes to be skipped in stream
        fis.skip(3);
        System.out.println(fis.getClass());
        while((i=fis.read())!=-1){
        int x = i;
        System.out.println(x);

 }
     }catch(Exception ex){
        ex.printStackTrace();
     }finally{
        if(fos!=null)
        //used to close system resources
           fos.close();
        if(fis!=null)
           fis.close();
     }
  }
}


Tuesday, June 21, 2016

Collections



Each key value pair is called as Entry , hence map is considered as collection of entry objects.
nulls not allowed in Map





Difference between == operator and .equals() method



In general == operator meant for reference comparision(address comparision)
where as .equals() method meant for content comparision.

Integer I1 = new Integer(10); I1=10

Integer I2 = new Integer(10);I2=10

Integer I3 = I1;

Sop(I1 == I2); // false

Sop(I1.equals(I2));//true

Sop(I3==I1);//true

Monday, June 20, 2016

Boxing and Unboxing

The automatic conversion of primitive data types into its equivalent Wrapper type is known as boxing and opposite operation is known as unboxing. This is the new feature of Java5. So java programmer doesn't need to write the conversion code.

  1. class BoxingExample1{  
  2.   public static void main(String args[]){  
  3.     int a=50;  
  4.         Integer a2=new Integer(a);//Boxing  
  5.   
  6.         Integer a3=5;//Boxing  
  7.           
  8.         System.out.println(a2+" "+a3);  
  9.  }   
  10. }

The automatic conversion of wrapper class type into corresponding primitive type, is known as Unboxing. Let's see the example of unboxing:
  1.     
  2. class UnboxingExample1{  
  3.   public static void main(String args[]){  
  4.     Integer i=new Integer(50);  
  5.         int a=i;  
  6.           
  7.         System.out.println(a);  
  8.  }   
  9. }