-
-
Notifications
You must be signed in to change notification settings - Fork 501
Виконані завдання з java-fundamentals-exercises #196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
0-0-intro/src/main/java/com/bobocode/intro/main.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
x = input() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 8 additions & 17 deletions
1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/Box.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,15 @@ | ||
package com.bobocode.basics; | ||
|
||
/** | ||
* {@link Box} is a container class that can store a value of any given type. Using Object as a field type | ||
* is flexible, because we can store anything we want there. But it is not safe, because it requires runtime casting | ||
* and there is no guarantee that we know the type of the stored value. | ||
* <p> | ||
* todo: refactor this class so it uses generic type "T" and run {@link com.bobocode.basics.BoxTest} to verify it | ||
*/ | ||
public class Box { | ||
private Object value; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
public Box(Object value) { | ||
this.value = value; | ||
} | ||
@Setter | ||
@Getter | ||
public class Box<T> { | ||
private T value; | ||
|
||
public Object getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(Object value) { | ||
public Box(T value) { | ||
this.value = value; | ||
} | ||
|
||
} |
13 changes: 2 additions & 11 deletions
1-0-java-basics/1-3-0-hello-generics/src/main/java/com/bobocode/basics/BoxDemoApp.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,12 @@ | ||
package com.bobocode.basics; | ||
|
||
/** | ||
* This demo demonstrates why using Object is not safe. It's not safe because runtime casting can cause runtime | ||
* exceptions. We should always fail as soon as possible. So in this code we should not allow setting String | ||
* value at compile time, if we expect to work with integers. | ||
* <p> | ||
* todo: refactor class {@link Box} to make type parameterization safe and make this demo fail at compile time | ||
*/ | ||
public class BoxDemoApp { | ||
public static void main(String[] args) { | ||
Box intBox = new Box(123); | ||
Box intBox2 = new Box(321); | ||
Box<Integer> intBox = new Box<>(123); | ||
Box<Integer> intBox2 = new Box<>(321); | ||
System.out.println((int) intBox.getValue() + (int) intBox2.getValue()); | ||
|
||
intBox.setValue(222); | ||
intBox.setValue("abc"); // this should not be allowed | ||
// the following code will compile, but will throw runtime exception | ||
System.out.println((int) intBox.getValue() + (int) intBox2.getValue()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.