Unicode System in java

Unicode System :

Unicode is a universal international standard character encoding which is capable of representing all the world’s written
languages.
Why java uses Unicode System?
Prior Unicode system, there were many language standards:

  • ASCII : It stand for American Standard Code for Information Interchange used for the United States.
  • ISO 8859-1 : It was used for Western European Language.
  • KOI-8 : Used for Russian.
  • GB18030 and BIG-5 : Used for chinese, and so on.

With these languages standard arise two problems:

  • A particular code value corresponds to different letters in the various language standards.
  • The encodings for languages with large character sets have variable length. Some common characters are encoded as single bytes, other require two or more byte.

To solve these problems, a new language standard was developed i.e. Unicode System.

In unicode, character holds 2 byte, so java uses 2 byte for characters.

  • lowest value are: \u0000 and
  • highest value are: \uFFFF
Advertisements

print triangle shape

Print triangle :

 


package com.pkjavacode.com;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
*
* @author pradeep
*/
public class TriangleShape {

public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Enter a number :");
int n = Integer.parseInt(br.readLine());
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n - i; j++) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Output :

Enter a number :
4
*
***
*****
*******
*********

Dependency Injection(DI) via constructor

Here is an example where you will see how to inject object via constructor.

Write Employee.java class with constructor and getter method.


package com.pkjavacode.com;

public class Employee {
String name;

public String getName() {
return name;
}

public int getSalary() {
return salary;
}

public String getCountry() {
return country;
}

int salary;
String country;

public Employee(String name, int salary, String country) {
this.name = name;
this.salary = salary;
this.country = country;
}

public String toString() {
return "Name :" + name + "Salary :" + salary + "Country :" + country;

}

public void getEmployeeDetails() {
System.out.println("Employee Name :" + getName() + ", Salary :"
+ getSalary() + ", Country :" + getCountry());
}

}

 

After that create EmployeeTest.java with main() method where you will have to create as follows.


package com.pkjavacode.com;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.pkjavacode.com.Employee;

public class EmployeeTest {

/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"beans.xml");
Employee emp = (Employee) context.getBean("emp");
emp.getEmployeeDetails();
}

}

 

 

Then create beans.xml file to configure.

 


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="emp" class="com.pkjavacode.com.Employee">
<constructor-arg value="Pradeep Yadav" />
<constructor-arg value="28000" />
<constructor-arg value="India" />
</bean>
</beans>

 

Output :

 

Employee Name :Pradeep Yadav,  Salary :28000,  Country :India

Dependency Injection(DI) via setter method

Dependency Injection(DI) via setter method is easy and take following steps:

First create a class with setter method :

Here SpringDemo.java class for that:-

package mypkg;
/**
*
* @author pradeep
*/
public class SpringDemo {
private String msg;
/**
* @return the msg
*/
public String getMsg() {
return msg;
}
/**
* @param msg the msg to set
*/
public void setMsg(String msg) {
this.msg = msg;
}

}

After that you have to create another class with main() method where you will test demo.

Here SpringTest.java class for that.

package mypkg;
import org.springframework.beans.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
*
* @author pradeep
*/
public class SpringTest {

public static void main(String args[]) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

SpringDemo ad = (SpringDemo) context.getBean("demo");
ad.getMsg();
}
}

After that you will have to create configuration file called beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

 <bean id="demo"
 class="mypkg.SpringDemo">
 <property name="msg" value="hello pradeep yadav"/>
 </bean>
</beans>