'Back-End/Java_1'에 해당되는 글 48건

donaricano-btn

Input by Scanner


- The Java Scanner class breaks the input into tokens using a delimiter that is whitespace by default.

- It provides many methods to read and parse various primitive values

- Java Scanner class is widely used to parse text for string and primitive types using regular expression


1. To get input from console



2. With delimiter

 

'Back-End > Java_1' 카테고리의 다른 글

[Java] Input by Console  (0) 2016.08.23
[Java] Input by BufferedReader  (0) 2016.08.22
[Java] This keyword - to refer current class instance variable  (0) 2016.08.05
[Java] Static_2  (0) 2016.07.26
[Java] Constructor_2  (0) 2016.07.26
블로그 이미지

리딩리드

,
donaricano-btn

This keyword


1. Usage of java this keyword

1) this keyword can be used to refer current class instance variable

2) this() can be used to invoke current class constructor

3) this keyword can be used to invoke current class method

4) this can be passed as an argument in the method call

5) this can be passed as an argument in the constructor call

6) this keyword can also be used to return the current class instance

2. Without this keyword


- We use a this key word to distinguish between local variable and instance variable

- we have a this 

 

- If local variables and instance variables are different, there is no need to use this keyword


'Back-End > Java_1' 카테고리의 다른 글

[Java] Input by BufferedReader  (0) 2016.08.22
[Java] Input by Scanner  (0) 2016.08.18
[Java] Static_2  (0) 2016.07.26
[Java] Constructor_2  (0) 2016.07.26
[Java] Method Overloading  (0) 2016.07.25
블로그 이미지

리딩리드

,

[Java] Static_2

Back-End/Java_1 2016. 7. 26. 18:22
donaricano-btn

Static_2

- The static keyword in java is used for memory management mainly

- It makes your program memory efficient


1) Static variable

- The static variable can be used to refer the common property of all objects 

- The static variable gets memory only once in class area at the time of class loading


1_1. Understanding problem without static variable


- Suppose there are 500 students in my college

- All instance data members will get memory each time when object is created

- If we make it static, college field will get memory only once


1_2. Example

 

1_3. Program Counter

1) Without static variable

 

- Since instance variable gets the memory at the time of object creation, each object will have the copy of the instance variable

- If it is incremented, it won't reflect to other objects

2) static variable

 

2) Static method

- A static method belongs to the class rather than object of class

- A static method can be invoked without the need for creating an instance of a class

- static method can access static data member and can change the value of it


2_1. Example

1) change

 

2) Calculate

 


3) Restriction

- The static method can not use non static data member or call non-static method directly

- this and super cannot be used in static context

 


 4) Why java main method is static?

Object is not required to call static method

- If it were non-static method, jvm create object first then call main() that will lead the problem of extra memory allocation


5) Java static block

- Is used to initialize the static data member

- It it executed before main method at the time of classloading

 


6) Can we execute a program without main() method?

- One of the way is static block but not in JDK 1.7

 

'Back-End > Java_1' 카테고리의 다른 글

[Java] Input by Scanner  (0) 2016.08.18
[Java] This keyword - to refer current class instance variable  (0) 2016.08.05
[Java] Constructor_2  (0) 2016.07.26
[Java] Method Overloading  (0) 2016.07.25
[Java] Object and Class  (0) 2016.06.27
블로그 이미지

리딩리드

,
donaricano-btn

Constructor_2

- Constructor is a special type of method that is used to initialize the object

- Constructor is invoked at the time of object creation


1. Rule

1) Constructor name must be same as its class name

2) Constructor must have no explicit return type


2. Type of constructor

1) Default constructor

2) Parameterized constructor


3. Default Constructor

3_1. Example


- If there is no constructor in a class, compiler automatically creates a default constructor


3_2. What is the purpose of default constructor?

- Default constructor provides the default values to the object like 0, null, etc

 


4. Parameterized Constructor

4_1. Why use parameterized constructor?

- Parameterized constructor is used to provide different values to the distinct objects

 


5. Copy Constructor

- There are many ways to copy the values of one object into another in java

1) By constructor

2) By assigning the values of one object into another

3) By clone() method of Object class


5_1. Example

 

'Back-End > Java_1' 카테고리의 다른 글

[Java] This keyword - to refer current class instance variable  (0) 2016.08.05
[Java] Static_2  (0) 2016.07.26
[Java] Method Overloading  (0) 2016.07.25
[Java] Object and Class  (0) 2016.06.27
[Java] Java OOPs Concepts  (0) 2016.06.27
블로그 이미지

리딩리드

,