Friday, 28 March 2014

Does Java pass by value or pass by reference?

Java does only pass by value and doesn't pass method arguments by reference, instead it passes Object reference by its value as method parameter. Java copies and passes the reference by value, not the object, hence if we modify Object values in method then original objects will get modified, since both references point to the original objects. 
Below are the 2 diagrams which we can see how primitive types work when we pass to another methods and how Objects work when we pass to another methods. Basically primitive types will create new variable in stack and it manipulates on it, but when we pass Objects it creates new reference and its passes the value of reference. Again Java does only pass by value and doesn't use pass by reference. 

When we pass primitive datatype to another method.
Does Java pass by value or pass by reference?
When we pass Object to another method.


Now lets see simple example in java as how it works. Lets create Employee class for Object passing.


public class Employee{
 
 private String name;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 } 
}

Next we will create main class which will initiate Employee Object and pass to another method along with integer primitive datatype.


public class MyTest {
 
 public static void main(String[] args) {
  
  Employee obj = new Employee();
  obj.setName("JAVA");// IMP
  
  int _int = 100;
  
  MyTest ob1 = new MyTest();
  ob1.changeValue(obj, _int);
  
  System.out.println("Inside Main Method : "+obj.getName()+ " : "+_int);
 }
 
 public void changeValue(Employee mObj, int mInt){
  
  mObj.setName("DISCOVER");
  
  mInt = 99;
  
  System.out.println("Inside another Method : "+mObj.getName() + " : "+mInt);
 }
 
}

OUTPUT:


Inside another Method : DISCOVER : 99
Inside Main Method : DISCOVER : 100

read more.. "Does Java pass by value or pass by reference?"

Wednesday, 26 March 2014

Character Array to String in Java

We may seen lot of arrays like Integer, Character, Float and Double or even String array. In this tutorial we will see about converting Character array to String in Java code. We can achieve this by 2 ways like given below,
Character Array to String in Java


public class CharToString {

 public static void main(String[] args) {
  
  char[] myArray = new char[]{'j','a','v','a',' ','d','i','s','c','o','v','e','r'};
  
  //By 2 ways we can convert char[] to String and lets see both ways
  
  String type1 = String.valueOf(myArray);

String type2 = new String(myArray);
  
  
  System.out.println("TYPE 1 : "+type1);
  System.out.println("TYPE 2 : "+type2);
 }
}

OUTPUT:

TYPE 1 : java discover
TYPE 2 : java discover

read more.. "Character Array to String in Java"

Tuesday, 25 March 2014

Java Locale to get Countries List

Some times we may required to list of country names from XML or through properties file. On alternate way we can get list of country names using Java API also which gives most effective and generic way of using country names along with their country codes. In this tutorial we will see about how to get list of Countries using Java util class Locale. They may be a simple code where lot of Java developers aware of it, but gives new learning for freshers to Java.
Java Locale to get Countries List



import java.util.Locale;

public class CountriesList {

 public static void main(String[] args) {
  
  String[] countriesList = Locale.getISOCountries();
  
  int id = 1;
  System.out.println("ID \t COUNTRY CODE \t COUNTRY NAME");
  
  for (String country : countriesList) {
   
   Locale tmp = new Locale("", country);
    
   System.out.println((id++) +" \t "+tmp.getCountry() + " \t\t " + tmp.getDisplayCountry());
  }
 }
}

OUTPUT:


ID   COUNTRY CODE   COUNTRY NAME
1   AD    Andorra
2   AE    United Arab Emirates
3   AF    Afghanistan
4   AG    Antigua and Barbuda
5   AI    Anguilla
6   AL    Albania
7   AM    Armenia
8   AN    Netherlands Antilles
9   AO    Angola
10   AQ    Antarctica
11   AR    Argentina
12   AS    American Samoa
13   AT    Austria
14   AU    Australia
15   AW    Aruba
16   AX    Åland Islands
17   AZ    Azerbaijan
18   BA    Bosnia and Herzegovina
19   BB    Barbados
20   BD    Bangladesh
21   BE    Belgium
22   BF    Burkina Faso
23   BG    Bulgaria
24   BH    Bahrain
25   BI    Burundi
26   BJ    Benin
27   BL    Saint Barthélemy
28   BM    Bermuda
29   BN    Brunei
30   BO    Bolivia
31   BQ    Bonaire, Sint Eustatius and Saba
32   BR    Brazil
33   BS    Bahamas
34   BT    Bhutan
35   BV    Bouvet Island
36   BW    Botswana
37   BY    Belarus
38   BZ    Belize
39   CA    Canada
40   CC    Cocos Islands
41   CD    The Democratic Republic Of Congo
42   CF    Central African Republic
43   CG    Congo
44   CH    Switzerland
45   CI    Côte d'Ivoire
46   CK    Cook Islands
47   CL    Chile
48   CM    Cameroon
49   CN    China
50   CO    Colombia
51   CR    Costa Rica
52   CU    Cuba
53   CV    Cape Verde
54   CW    Curaçao
55   CX    Christmas Island
56   CY    Cyprus
57   CZ    Czech Republic
58   DE    Germany
59   DJ    Djibouti
60   DK    Denmark
61   DM    Dominica
62   DO    Dominican Republic
63   DZ    Algeria
64   EC    Ecuador
65   EE    Estonia
66   EG    Egypt
67   EH    Western Sahara
68   ER    Eritrea
69   ES    Spain
70   ET    Ethiopia
71   FI    Finland
72   FJ    Fiji
73   FK    Falkland Islands
74   FM    Micronesia
75   FO    Faroe Islands
76   FR    France
77   GA    Gabon
78   GB    United Kingdom
79   GD    Grenada
80   GE    Georgia
81   GF    French Guiana
82   GG    Guernsey
83   GH    Ghana
84   GI    Gibraltar
85   GL    Greenland
86   GM    Gambia
87   GN    Guinea
88   GP    Guadeloupe
89   GQ    Equatorial Guinea
90   GR    Greece
91   GS    South Georgia And The South Sandwich Islands
92   GT    Guatemala
93   GU    Guam
94   GW    Guinea-Bissau
95   GY    Guyana
96   HK    Hong Kong
97   HM    Heard Island And McDonald Islands
98   HN    Honduras
99   HR    Croatia
100   HT    Haiti
101   HU    Hungary
102   ID    Indonesia
103   IE    Ireland
104   IL    Israel
105   IM    Isle Of Man
106   IN    India
107   IO    British Indian Ocean Territory
108   IQ    Iraq
109   IR    Iran
110   IS    Iceland
111   IT    Italy
112   JE    Jersey
113   JM    Jamaica
114   JO    Jordan
115   JP    Japan
116   KE    Kenya
117   KG    Kyrgyzstan
118   KH    Cambodia
119   KI    Kiribati
120   KM    Comoros
121   KN    Saint Kitts And Nevis
122   KP    North Korea
123   KR    South Korea
124   KW    Kuwait
125   KY    Cayman Islands
126   KZ    Kazakhstan
127   LA    Laos
128   LB    Lebanon
129   LC    Saint Lucia
130   LI    Liechtenstein
131   LK    Sri Lanka
132   LR    Liberia
133   LS    Lesotho
134   LT    Lithuania
135   LU    Luxembourg
136   LV    Latvia
137   LY    Libya
138   MA    Morocco
139   MC    Monaco
140   MD    Moldova
141   ME    Montenegro
142   MF    Saint Martin
143   MG    Madagascar
144   MH    Marshall Islands
145   MK    Macedonia
146   ML    Mali
147   MM    Myanmar
148   MN    Mongolia
149   MO    Macao
150   MP    Northern Mariana Islands
151   MQ    Martinique
152   MR    Mauritania
153   MS    Montserrat
154   MT    Malta
155   MU    Mauritius
156   MV    Maldives
157   MW    Malawi
158   MX    Mexico
159   MY    Malaysia
160   MZ    Mozambique
161   NA    Namibia
162   NC    New Caledonia
163   NE    Niger
164   NF    Norfolk Island
165   NG    Nigeria
166   NI    Nicaragua
167   NL    Netherlands
168   NO    Norway
169   NP    Nepal
170   NR    Nauru
171   NU    Niue
172   NZ    New Zealand
173   OM    Oman
174   PA    Panama
175   PE    Peru
176   PF    French Polynesia
177   PG    Papua New Guinea
178   PH    Philippines
179   PK    Pakistan
180   PL    Poland
181   PM    Saint Pierre And Miquelon
182   PN    Pitcairn
183   PR    Puerto Rico
184   PS    Palestine
185   PT    Portugal
186   PW    Palau
187   PY    Paraguay
188   QA    Qatar
189   RE    Reunion
190   RO    Romania
191   RS    Serbia
192   RU    Russia
193   RW    Rwanda
194   SA    Saudi Arabia
195   SB    Solomon Islands
196   SC    Seychelles
197   SD    Sudan
198   SE    Sweden
199   SG    Singapore
200   SH    Saint Helena
201   SI    Slovenia
202   SJ    Svalbard And Jan Mayen
203   SK    Slovakia
204   SL    Sierra Leone
205   SM    San Marino
206   SN    Senegal
207   SO    Somalia
208   SR    Suriname
209   ST    Sao Tome And Principe
210   SV    El Salvador
211   SX    Sint Maarten (Dutch part)
212   SY    Syria
213   SZ    Swaziland
214   TC    Turks And Caicos Islands
215   TD    Chad
216   TF    French Southern Territories
217   TG    Togo
218   TH    Thailand
219   TJ    Tajikistan
220   TK    Tokelau
221   TL    Timor-Leste
222   TM    Turkmenistan
223   TN    Tunisia
224   TO    Tonga
225   TR    Turkey
226   TT    Trinidad and Tobago
227   TV    Tuvalu
228   TW    Taiwan
229   TZ    Tanzania
230   UA    Ukraine
231   UG    Uganda
232   UM    United States Minor Outlying Islands
233   US    United States
234   UY    Uruguay
235   UZ    Uzbekistan
236   VA    Vatican
237   VC    Saint Vincent And The Grenadines
238   VE    Venezuela
239   VG    British Virgin Islands
240   VI    U.S. Virgin Islands
241   VN    Vietnam
242   VU    Vanuatu
243   WF    Wallis And Futuna
244   WS    Samoa
245   YE    Yemen
246   YT    Mayotte
247   ZA    South Africa
248   ZM    Zambia
249   ZW    Zimbabwe

read more.. "Java Locale to get Countries List"