In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to use the initialization of java variables". In the daily operation, I believe many people have doubts about how to use the initialization of java variables. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how to use the initialization of java variables". Next, please follow the editor to study!
1. After you declare a variable, you must use an assignment statement to explicitly initialize the variable.
2. The variable declaration is as close as possible to where the variable is used for the first time.
3. For local variables, if the type of the variable can be inferred from its initial value, there is no need to declare the type.
You only need to use the keyword var without specifying the type.
Example
/ / Java tries to ensure that all variables are properly initialized before use, otherwise exception / / Java provides four initialization methods: default initialization, explicit assignment initialization, calling class object initialization and calling method initialization. / / incorrect void f () {int I; iComplete;} / / an exception will be reported during execution because variable I is not initialized # # one of the member variable initialization methods-default initialization: public class InitialValues {boolean t; char c; byte b; short s; int i; long l; float f; double d; InitialValues reference Void printInitialValues () {System.out.println ("Data type Initial value"); System.out.println ("boolean" + t); System.out.println ("char [" + c + "]"); System.out.println ("byte" + b); System.out.println ("short" + s); System.out.println ("int" + I) System.out.println ("long" + l); System.out.println ("float" + f); System.out.println ("double" + d); System.out.println ("reference" + reference);} public static void main (String [] args) {new InitialValues () .printInitialValues () }} output: the default value of Data type Initial valueboolean false char [] / / char is 0 or written as'\ u000000', so it is displayed as blank byte 0short 0int 0long 0 / / 0Lfloat 0 / / 0.0Fdouble 0.0 reference null # # member variable initialization method 2-explicit assignment: public class InitialValues2 {boolean bool = true; char ch = 'x'; byte b = 47; short s = 0xff; int I = 999; long lng = 1; float f = 3.14f Double d = 3.14159;} # # the third way to initialize member variables-call the object for initialization assignment: class Depth {} public class Measurement {Depth d = new Depth (); / /.} / / Note: if you try to use d without giving it an initial value, a run-time error and an exception will occur. # # the fourth initialization method for member variables: / / 1, call the nonparametric method public class MethodInit {int I = f (); int f () {return 11;}} / / 2, call the parameterized method public class MethodInit2 {int I = f (); int j = g (I); int f () {return 11 } int g (int n) {return n * 10;} / Note: when calling a parameterized method, the parameters in the method cannot be uninitialized class member variables, otherwise an exception will be reported, and the study on "how to initialize java variables" will be over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.