├── .gitignore ├── .mvn └── wrapper │ ├── MavenWrapperDownloader.java │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── README.md ├── mvnw ├── mvnw.cmd ├── pom.xml ├── src ├── main │ ├── java │ │ └── com │ │ │ └── effectivejava │ │ │ └── tutorial │ │ │ └── effectivejava │ │ │ ├── EffectiveJavaApplication.java │ │ │ ├── item1 │ │ │ ├── Dondurma.java │ │ │ ├── Fenerbahce.java │ │ │ ├── Item1.java │ │ │ ├── ItemOneTest.java │ │ │ ├── Pide.java │ │ │ └── Yemek.java │ │ │ ├── item2 │ │ │ ├── ItemTwoTest.java │ │ │ ├── Starbucks.java │ │ │ └── StarbucksBuilder.java │ │ │ ├── item3 │ │ │ ├── Cache.java │ │ │ ├── CacheSingleton.java │ │ │ ├── DefineSingleton.java │ │ │ ├── EagerCache.java │ │ │ ├── ItemThreeTest.java │ │ │ └── LazyCache.java │ │ │ ├── item4 │ │ │ ├── Dene.java │ │ │ ├── DenemeAbs.java │ │ │ ├── Enum.java │ │ │ └── ItemFourTest.java │ │ │ ├── item5 │ │ │ ├── ItemFiveTest.java │ │ │ ├── Sözlük.java │ │ │ ├── YazıAnaliziDependencyInjection.java │ │ │ ├── YazıAnaliziSingleton.java │ │ │ └── YazıAnaliziUtility.java │ │ │ ├── item6 │ │ │ ├── ItemSixTest.java │ │ │ └── RegExample.java │ │ │ ├── item7 │ │ │ ├── GarbageCollection.java │ │ │ ├── ItemSevenTest.java │ │ │ ├── ItemSevenTest2.java │ │ │ ├── ItemSevenTest3.java │ │ │ └── Stack.java │ │ │ ├── item8a9 │ │ │ ├── ItemEightTest.java │ │ │ └── test.txt │ │ │ ├── itema1011 │ │ │ ├── ColorPoint.java │ │ │ ├── ColorPointInherit.java │ │ │ ├── DenemeSınıf.java │ │ │ ├── DenemeSınıfOvrd.java │ │ │ ├── ItemTenTest.java │ │ │ └── Point.java │ │ │ ├── itema12 │ │ │ ├── ItemTwelveTest.java │ │ │ ├── Request.java │ │ │ └── Virus.java │ │ │ ├── itema13 │ │ │ ├── ItemThirteenTest.java │ │ │ ├── PhoneNumber.java │ │ │ └── Stack.java │ │ │ ├── itema14 │ │ │ ├── CaseInsensitiveString.java │ │ │ ├── IsımKıyaslama.java │ │ │ ├── ItemFourteenTest.java │ │ │ ├── Kitap.java │ │ │ └── PhoneNumber.java │ │ │ ├── itema15 │ │ │ ├── GuvenliKod.java │ │ │ └── ItemFifthteenTest.java │ │ │ ├── itema16 │ │ │ ├── Futbol.java │ │ │ ├── ItemSixteenTest.java │ │ │ └── Time.java │ │ │ ├── itema17 │ │ │ ├── Complex.java │ │ │ ├── Immutable.java │ │ │ ├── Mutable.java │ │ │ └── TestDrive.java │ │ │ ├── itema18 │ │ │ ├── Hayvan.java │ │ │ ├── Kedi.java │ │ │ ├── Kitap.java │ │ │ ├── KompozisyonDüzeltmesi.java │ │ │ ├── KötüBirKalıtımÖrneği.java │ │ │ ├── Kütüphane.java │ │ │ └── TestDrive.java │ │ │ ├── itema19 │ │ │ ├── AnaSınıf.java │ │ │ ├── CocukSınıf.java │ │ │ ├── KalıtımKaçış.java │ │ │ ├── KalıtımÇocuk.java │ │ │ ├── Sub.java │ │ │ ├── Super.java │ │ │ └── TestDrive.java │ │ │ ├── itema20 │ │ │ ├── AbstractMapEntry.java │ │ │ ├── Hayvan.java │ │ │ ├── IntArrays.java │ │ │ ├── Kuş.java │ │ │ ├── TestDrive.java │ │ │ ├── ŞarkıYazarı.java │ │ │ ├── Şarkıcı.java │ │ │ └── ŞarkıcıŞarkıYazarı.java │ │ │ ├── itema21 │ │ │ └── TestDrive.java │ │ │ ├── itema22 │ │ │ ├── DenemeSınıf.java │ │ │ ├── Kus.java │ │ │ ├── PhysicalConstant.java │ │ │ ├── PhysicalConstants.java │ │ │ ├── TestDrive.java │ │ │ └── UcmaYetisi.java │ │ │ ├── itema23 │ │ │ ├── Circle.java │ │ │ ├── Figure.java │ │ │ ├── FigureTrue.java │ │ │ ├── Rectangle.java │ │ │ ├── Square.java │ │ │ └── TestDrive.java │ │ │ ├── itema24 │ │ │ ├── LocalClass.java │ │ │ ├── NonStaticNested.java │ │ │ ├── StaticNested.java │ │ │ ├── SuperClass.java │ │ │ ├── SuperClassDerin.java │ │ │ └── TestDrive.java │ │ │ ├── itema25 │ │ │ ├── Dessert.java │ │ │ ├── Test.java │ │ │ ├── TestDrive.java │ │ │ └── Utensil.java │ │ │ ├── itema26 │ │ │ ├── Raw.java │ │ │ └── TestDrive.java │ │ │ ├── itema27 │ │ │ └── TestDrive.java │ │ │ ├── itema28 │ │ │ └── TestDrive.java │ │ │ ├── itema29 │ │ │ ├── EmptyStackException.java │ │ │ ├── StackTechOne.java │ │ │ ├── StackTechTwo.java │ │ │ └── TestDrive.java │ │ │ ├── itema30 │ │ │ ├── GenericSingletonFactory.java │ │ │ ├── TestDrive.java │ │ │ └── Union.java │ │ │ ├── itema31 │ │ │ ├── A.java │ │ │ ├── B.java │ │ │ ├── C.java │ │ │ ├── Chooser.java │ │ │ ├── D.java │ │ │ ├── RecursiveTypeBound.java │ │ │ ├── Stack.java │ │ │ ├── Swap.java │ │ │ ├── TestDrive.java │ │ │ ├── TestDrive2.java │ │ │ └── Union.java │ │ │ ├── itema32 │ │ │ ├── Dangerous.java │ │ │ ├── FlattenWithList.java │ │ │ ├── FlattenWithVarargs.java │ │ │ ├── PickTwo.java │ │ │ ├── SafePickTwo.java │ │ │ └── TestDrive.java │ │ │ ├── itema33 │ │ │ ├── Column.java │ │ │ ├── DatabaseClient.java │ │ │ ├── DatabaseRow.java │ │ │ ├── Favorites.java │ │ │ ├── FavoritesContract.java │ │ │ ├── PrintAnnotation.java │ │ │ └── TestDrive.java │ │ │ ├── itema34 │ │ │ ├── Inverse.java │ │ │ ├── Operation.java │ │ │ ├── OperationSwitch.java │ │ │ ├── PayrollDay.java │ │ │ ├── Planet.java │ │ │ ├── TestDrive.java │ │ │ └── WeightTable.java │ │ │ ├── itema35 │ │ │ ├── Ensemble.java │ │ │ ├── OrdinalExample.java │ │ │ └── TestDrive.java │ │ │ ├── itema36 │ │ │ ├── TestDrive.java │ │ │ ├── Text.java │ │ │ └── TextBad.java │ │ │ ├── itema37 │ │ │ ├── Phase.java │ │ │ ├── Plant.java │ │ │ └── TestDrive.java │ │ │ ├── itema38 │ │ │ ├── BasicOperation.java │ │ │ ├── ExtendedOperation.java │ │ │ ├── Operation.java │ │ │ └── TestDrive.java │ │ │ ├── itema39 │ │ │ ├── TestDrive.java │ │ │ ├── annotationwitharrayparameter │ │ │ │ ├── ExceptionTest.java │ │ │ │ ├── RunTests.java │ │ │ │ └── Sample3.java │ │ │ ├── annotationwithparameter │ │ │ │ ├── ExceptionTest.java │ │ │ │ ├── RunTests.java │ │ │ │ └── Sample2.java │ │ │ ├── markerannotation │ │ │ │ ├── RunTests.java │ │ │ │ ├── Sample.java │ │ │ │ └── Test.java │ │ │ ├── reflection │ │ │ │ ├── Metallica.java │ │ │ │ └── TestReflection.java │ │ │ └── repeatableannotation │ │ │ │ ├── ExceptionTest.java │ │ │ │ ├── ExceptionTestContainer.java │ │ │ │ ├── RunTests.java │ │ │ │ └── Sample4.java │ │ │ ├── itema40 │ │ │ ├── Bigram.java │ │ │ ├── Bigram2.java │ │ │ ├── TestDrive.java │ │ │ └── TestDriveExtended.java │ │ │ ├── itema41 │ │ │ ├── A.java │ │ │ ├── Ajdar.java │ │ │ ├── Kaliteli.java │ │ │ ├── Metallica.java │ │ │ ├── Pentegram.java │ │ │ ├── SonTest.java │ │ │ └── TestDrive.java │ │ │ ├── itema42 │ │ │ ├── Operation.java │ │ │ ├── SortFourWays.java │ │ │ ├── TestDrive.java │ │ │ └── lambda │ │ │ │ ├── LambdaExample.java │ │ │ │ ├── Yazdır.java │ │ │ │ └── YazdırParametre.java │ │ │ ├── itema43 │ │ │ ├── Freq.java │ │ │ ├── FunctionalString.java │ │ │ └── TestDrive.java │ │ │ ├── itema44 │ │ │ ├── KıyaslaBuyuguAl.java │ │ │ └── TestDrive.java │ │ │ ├── itema45 │ │ │ ├── Card.java │ │ │ ├── CharStream.java │ │ │ ├── MersennePrimes.java │ │ │ ├── TestDrive.java │ │ │ └── anagrams │ │ │ │ ├── HybridAnagrams.java │ │ │ │ ├── IterativeAnagrams.java │ │ │ │ ├── StreamAnagrams.java │ │ │ │ └── words │ │ │ ├── itema46 │ │ │ ├── Freq.java │ │ │ └── TestDrive.java │ │ │ ├── itema47 │ │ │ ├── Adapters.java │ │ │ ├── PowerSet.java │ │ │ ├── SubLists.java │ │ │ └── TestDrive.java │ │ │ ├── itema48 │ │ │ ├── ParallelMersennePrimes.java │ │ │ ├── ParallelPrimeCounting.java │ │ │ └── TestDrive.java │ │ │ ├── itema49 │ │ │ ├── NullCheck.java │ │ │ └── TestDrive.java │ │ │ ├── itema50 │ │ │ ├── Attacks.java │ │ │ ├── Period.java │ │ │ └── TestDrive.java │ │ │ ├── itema51 │ │ │ ├── TemperatureScale.java │ │ │ └── TestDrive.java │ │ │ ├── itema52 │ │ │ ├── Champagne.java │ │ │ ├── CollectionClassifier.java │ │ │ ├── FixedCollectionClassifier.java │ │ │ ├── Overriding.java │ │ │ ├── SetList.java │ │ │ ├── SparklingWine.java │ │ │ ├── TestDrive.java │ │ │ └── Wine.java │ │ │ ├── itema53 │ │ │ ├── TestDrive.java │ │ │ └── Varargs.java │ │ │ ├── itema54 │ │ │ ├── Cheese.java │ │ │ └── TestDrive.java │ │ │ ├── itema55 │ │ │ ├── Insan.java │ │ │ ├── Max.java │ │ │ ├── ParentPid.java │ │ │ └── TestDrive.java │ │ │ ├── itema56 │ │ │ ├── DocExamples.java │ │ │ └── TestDrive.java │ │ │ ├── itema57 │ │ │ └── TestDrive.java │ │ │ ├── itema58 │ │ │ ├── Card.java │ │ │ ├── DiceRolls.java │ │ │ └── TestDrive.java │ │ │ ├── itema59 │ │ │ ├── Curl.java │ │ │ ├── R.java │ │ │ ├── RandomBug.java │ │ │ └── TestDrive.java │ │ │ ├── itema60 │ │ │ ├── BigDecimalChange.java │ │ │ ├── Change.java │ │ │ ├── IntChange.java │ │ │ └── TestDrive.java │ │ │ ├── itema61 │ │ │ ├── BrokenComparator.java │ │ │ ├── TestDrive.java │ │ │ └── Unbelievable.java │ │ │ ├── itema62 │ │ │ ├── TestDrive.java │ │ │ └── XapiModel.java │ │ │ ├── itema63 │ │ │ └── TestDrive.java │ │ │ ├── itema64 │ │ │ └── TestDrive.java │ │ │ ├── itema65b66c66d67e68 │ │ │ ├── ReflectiveInstantiation.java │ │ │ └── TestDrive.java │ │ │ ├── itema69toHalfOfCapter10 │ │ │ ├── ArrayOperations.java │ │ │ ├── TestDrive.java │ │ │ ├── TestDriveSupport.java │ │ │ └── TestDriveSupport2.java │ │ │ ├── itema69toRestOfCapter10 │ │ │ ├── IncorrectFileExtensionException.java │ │ │ ├── IncorrectFileNameException.java │ │ │ ├── IndexOutOfBoundsException.java │ │ │ ├── InvalidBirthdayException.java │ │ │ └── TestDrive.java │ │ │ ├── itema78 │ │ │ ├── StopThread1.java │ │ │ ├── StopThread2.java │ │ │ ├── StopThread3.java │ │ │ ├── StopThread4.java │ │ │ └── TestDrive.java │ │ │ ├── itema79 │ │ │ ├── DeadLock.java │ │ │ ├── DeadLockSolution.java │ │ │ ├── TestDrive.java │ │ │ └── bookexample │ │ │ │ ├── ForwardingSet.java │ │ │ │ ├── ObservableSet.java │ │ │ │ ├── SetObserver.java │ │ │ │ ├── Test1.java │ │ │ │ ├── Test2.java │ │ │ │ └── Test3.java │ │ │ ├── itema80 │ │ │ ├── CachedThreadPoolExecutorExample.java │ │ │ ├── FixedThreadPoolExample.java │ │ │ ├── ScheduledThreadPoolExecutorExample.java │ │ │ ├── SingleThreadExecutorExample.java │ │ │ ├── TestDrive.java │ │ │ └── WithouhSingleThread.java │ │ │ ├── itema81 │ │ │ ├── ConcurrentTimer.java │ │ │ ├── Intern.java │ │ │ ├── TestDrive.java │ │ │ ├── concurrentexample │ │ │ │ ├── BlockingQueueExample.java │ │ │ │ ├── ConcurrentHashmapExample.java │ │ │ │ └── CountDownLatchExample.java │ │ │ └── waitandnotify │ │ │ │ ├── Data.java │ │ │ │ ├── NetworkDriver.java │ │ │ │ ├── Receiver.java │ │ │ │ └── Sender.java │ │ │ ├── itema82b83c84 │ │ │ ├── TestDrive.java │ │ │ ├── item83 │ │ │ │ ├── FieldType.java │ │ │ │ └── Initialization.java │ │ │ └── item84 │ │ │ │ └── SlowCountDownLatch.java │ │ │ ├── itema85b86 │ │ │ ├── DeserializationBomb.java │ │ │ ├── TestDrive.java │ │ │ ├── User.java │ │ │ ├── UserJson.java │ │ │ ├── Util.java │ │ │ ├── user.json │ │ │ └── user.txt │ │ │ ├── itema87 │ │ │ ├── Bad.java │ │ │ ├── BadBecomeGood.java │ │ │ ├── Good.java │ │ │ ├── MyExample.java │ │ │ ├── MyExample.txt │ │ │ ├── StringList.java │ │ │ └── TestDrive.java │ │ │ ├── itema88b89 │ │ │ ├── Elvis.java │ │ │ ├── ElvisE.java │ │ │ ├── ElvisImpersonator.java │ │ │ └── TestDrive.java │ │ │ └── itema90 │ │ │ ├── Period.java │ │ │ ├── SerializationProxy.java │ │ │ ├── TestDrive.java │ │ │ ├── TruePerson.java │ │ │ ├── TruePerson.txt │ │ │ └── WrongPerson.java │ └── resources │ │ └── application.properties └── test │ └── java │ └── com │ └── effectivejava │ └── tutorial │ └── effectivejava │ └── EffectiveJavaApplicationTests.java └── xyz.txt /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsonmez10/effective-java-deep-tutorial/96df8da87e792e125453c5906641153a1de2b672/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip 2 | wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # effective-java-deep-tutorial 2 | 3 | *You can reach full tutorials from: https://www.udemy.com/course/ileriorta-seviye-java-dersleri-effective-java/?referralCode=79601D0E5CF15BE0AD3B* 4 | *You can reach free tutorials from: https://www.youtube.com/watch?v=h7gp2_Z4Lms&list=PLb37mF_7iAbCut-dtuLB-66QPbITw1S9y* 5 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/EffectiveJavaApplication.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | import com.effectivejava.tutorial.effectivejava.item7.Stack; 7 | 8 | @SpringBootApplication 9 | public class EffectiveJavaApplication { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(EffectiveJavaApplication.class, args); 13 | 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item1/Dondurma.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item1; 2 | 3 | public class Dondurma extends Yemek{ 4 | 5 | 6 | } 7 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item1/Fenerbahce.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item1; 2 | 3 | public class Fenerbahce { 4 | 5 | private String yeniFutbolcu; 6 | private int age; 7 | 8 | public Fenerbahce(String yeniFutbolcu, int age) { 9 | // TODO Auto-generated constructor stub 10 | this.yeniFutbolcu = yeniFutbolcu; 11 | this.age = age; 12 | } 13 | 14 | public Fenerbahce(String yeniTeknikDirektor) { 15 | // TODO Auto-generated constructor stub 16 | this.yeniFutbolcu = yeniFutbolcu; 17 | } 18 | 19 | 20 | public static Fenerbahce yeniFutbolcuUret(String yeniFutbolcu) 21 | { 22 | return new Fenerbahce(yeniFutbolcu, 22); 23 | } 24 | 25 | 26 | 27 | 28 | 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item1/Item1.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item1; 2 | 3 | import java.util.Collections; 4 | import java.util.List; 5 | 6 | public class Item1 { 7 | 8 | public static void main(String[] args) { 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item1/ItemOneTest.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item1; 2 | 3 | public class ItemOneTest { 4 | 5 | public static void main(String[] args) { 6 | 7 | 8 | 9 | 10 | 11 | 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item1/Pide.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item1; 2 | 3 | public class Pide extends Yemek { 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item1/Yemek.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item1; 2 | 3 | public class Yemek { 4 | 5 | String name; 6 | 7 | 8 | 9 | public static Yemek soğukYemek(String name) 10 | { 11 | // Soğuk yemek 12 | return new Dondurma(); 13 | } 14 | 15 | public static Yemek sıcakYemek(String name) 16 | { 17 | return new Pide(); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item2/ItemTwoTest.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item2; 2 | 3 | public class ItemTwoTest { 4 | 5 | 6 | public static void main(String[] args) { 7 | 8 | 9 | StarbucksBuilder sb = new StarbucksBuilder.Builder("tall").yumuşakİçenler("ben yumuşak istiyorum").build(); 10 | 11 | 12 | 13 | 14 | } 15 | 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item2/Starbucks.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item2; 2 | 3 | public class Starbucks { 4 | 5 | // Gerekli 6 | private String kahveBoyutu; 7 | 8 | // Seçime göre değişir 9 | private String laktozsuzSütMiktarı; 10 | private String yumuşatıcı; 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item2/StarbucksBuilder.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item2; 2 | 3 | public class StarbucksBuilder { 4 | 5 | // Gerekli 6 | private String kahveBoyutu; 7 | 8 | // Seçime göre değişir 9 | private String laktozsuzSütMiktarı; 10 | private String yumuşatıcı; 11 | 12 | 13 | 14 | public static class Builder{ 15 | 16 | // Gerekli 17 | private final String kahveBoyutu; 18 | 19 | // Seçime göre değişir 20 | private String laktozsuzSütMiktarı; 21 | private String yumuşatıcı; 22 | 23 | 24 | public Builder(String kahveBoyutu) { 25 | super(); 26 | this.kahveBoyutu = kahveBoyutu; 27 | } 28 | 29 | public Builder laktozsuzTayfa(String laktozsuzSut) 30 | { 31 | laktozsuzSütMiktarı = laktozsuzSut; 32 | return this; 33 | } 34 | 35 | public Builder yumuşakİçenler(String yumuşakDeğer) 36 | { 37 | yumuşatıcı = yumuşakDeğer; 38 | return this; 39 | } 40 | 41 | 42 | public StarbucksBuilder build() 43 | { 44 | return new StarbucksBuilder(this); 45 | } 46 | 47 | } 48 | 49 | public StarbucksBuilder(Builder builder) { 50 | // TODO Auto-generated constructor stub 51 | kahveBoyutu = builder.kahveBoyutu; 52 | laktozsuzSütMiktarı = builder.laktozsuzSütMiktarı; 53 | yumuşatıcı = builder.yumuşatıcı; 54 | 55 | } 56 | 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item3/Cache.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item3; 2 | 3 | public interface Cache { 4 | 5 | public void put(F key, S value); 6 | public S get(F key); 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item3/CacheSingleton.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item3; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public enum CacheSingleton implements Cache{ 7 | INSTANCE; 8 | 9 | private Map map = new HashMap(); 10 | 11 | public void put(Object key, Object value) 12 | { 13 | map.put(key, value); 14 | } 15 | 16 | public Object get(Object key) 17 | { 18 | return map.get(key); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item3/DefineSingleton.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item3; 2 | 3 | public class DefineSingleton { 4 | 5 | public static DefineSingleton ds = new DefineSingleton(); 6 | 7 | private DefineSingleton() { 8 | // TODO Auto-generated constructor stub 9 | } 10 | 11 | 12 | public static DefineSingleton getInstance() 13 | { 14 | return ds; 15 | } 16 | 17 | 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item3/EagerCache.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item3; 2 | 3 | import java.util.HashMap; 4 | 5 | public class EagerCache implements Cache { 6 | 7 | private static final EagerCache instance = new EagerCache(); 8 | private HashMap map; 9 | 10 | private EagerCache() { 11 | map = new HashMap(); 12 | } 13 | 14 | public void put(Object key, Object value) { 15 | map = new HashMap(); 16 | } 17 | 18 | public Object get(Object key) { 19 | return map.get(key); 20 | } 21 | 22 | public static EagerCache getInstance(){ 23 | return instance; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item3/ItemThreeTest.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item3; 2 | 3 | public class ItemThreeTest { 4 | 5 | public static void main(String[] args) { 6 | 7 | 8 | new Thread(new Runnable() { 9 | 10 | @Override 11 | public void run() { 12 | // TODO Auto-generated method stub 13 | try { 14 | Thread.sleep(1); 15 | } catch (InterruptedException e) { 16 | // TODO: handle exception 17 | } 18 | 19 | System.out.println("HashCode of LazyCache: " + LazyCache.getInstance()); 20 | System.out.println("HashCode of EagerCache: " + EagerCache.getInstance()); 21 | System.out.println("HashCode of CacheSingleton: " + CacheSingleton.INSTANCE.hashCode()); 22 | 23 | } 24 | }).start(); 25 | 26 | new Thread(new Runnable() { 27 | 28 | @Override 29 | public void run() { 30 | // TODO Auto-generated method stub 31 | 32 | System.out.println("HashCode of LazyCache: " + LazyCache.getInstance()); 33 | System.out.println("HashCode of EagerCache: " + EagerCache.getInstance()); 34 | System.out.println("HashCode of CacheSingleton: " + CacheSingleton.INSTANCE.hashCode()); 35 | 36 | } 37 | }).start(); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item3/LazyCache.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item3; 2 | 3 | import java.util.HashMap; 4 | 5 | public class LazyCache implements Cache { 6 | 7 | private static LazyCache instance; 8 | private HashMap map; 9 | 10 | private LazyCache() 11 | { 12 | map = new HashMap(); 13 | } 14 | 15 | public void put(Object key, Object value) { 16 | map = new HashMap(); 17 | } 18 | 19 | public Object get(Object key) { 20 | return map.get(key); 21 | } 22 | 23 | public static LazyCache getInstance(){ 24 | if(instance == null) 25 | { 26 | instance = new LazyCache(); 27 | } 28 | return instance; 29 | } 30 | 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item4/Dene.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item4; 2 | 3 | public class Dene extends DenemeAbs{ 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item4/DenemeAbs.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item4; 2 | 3 | public abstract class DenemeAbs { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item4/Enum.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item4; 2 | 3 | public enum Enum { 4 | ; 5 | 6 | 7 | public static int topla() 8 | { 9 | return 1; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item4/ItemFourTest.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item4; 2 | 3 | import java.util.Collections; 4 | 5 | // org.springframework.util.StringUtils 6 | 7 | public class ItemFourTest { 8 | 9 | public static void main(String[] args) { 10 | 11 | Dene d = new Dene(); 12 | Enum.topla(); 13 | 14 | } 15 | 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item5/ItemFiveTest.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item5; 2 | 3 | import java.time.LocalDate; 4 | import java.util.function.Supplier; 5 | 6 | public class ItemFiveTest { 7 | 8 | public static void main(String[] args) { 9 | 10 | // Supplier supplier = ()-> "Furkan"; 11 | // 12 | // System.out.println(supplier.get()); 13 | // 14 | // Supplier türkçe = ()-> new Sözlük("Türkçe"); 15 | // System.out.println(türkçe.get().toString()); 16 | // 17 | // YazıAnaliziDependencyInjection as = new YazıAnaliziDependencyInjection(türkçe); 18 | // System.out.println(as.isValid()); 19 | // 20 | 21 | 22 | Supplier randomValue = () -> Math.random(); 23 | 24 | double random = Math.random(); 25 | System.out.println("Bir " + random); 26 | System.out.println("iki " + Math.random()); 27 | System.out.println("üç " +randomValue.get()); 28 | System.out.println("dört " +randomValue.get()); 29 | 30 | randomValue.get(); 31 | Math.random(); 32 | 33 | Supplier dahaKısa = () -> YazıAnaliziDependencyInjection.upuzunBayağıUzunBirMetodYazıyorumŞuAnda(); 34 | 35 | dahaKısa.get(); 36 | 37 | 38 | 39 | Supplier s1 = LocalDate::now; 40 | LocalDate s2 = LocalDate.now(); 41 | 42 | System.out.println(s1.get()); //2016-10-25 43 | System.out.println(s2); //2016-10-25 44 | 45 | 46 | 47 | 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item5/Sözlük.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item5; 2 | 3 | public class Sözlük { 4 | 5 | private String dil; 6 | 7 | public Sözlük(String dil) { 8 | // TODO Auto-generated constructor stub 9 | this.dil = dil; 10 | } 11 | 12 | public static Boolean isValid() 13 | { 14 | return true; 15 | } 16 | 17 | 18 | @Override 19 | public String toString() { 20 | // TODO Auto-generated method stub 21 | return dil; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item5/YazıAnaliziDependencyInjection.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item5; 2 | 3 | import java.util.Objects; 4 | import java.util.function.Supplier; 5 | 6 | public class YazıAnaliziDependencyInjection { 7 | 8 | private final Sözlük sözlük; 9 | 10 | // public YazıAnaliziDependencyInjection(Supplier sözlük) { 11 | // // TODO Auto-generated constructor stub 12 | // this.sözlük = sözlük.get(); 13 | // } 14 | 15 | public YazıAnaliziDependencyInjection(Sözlük sözlük) { 16 | // TODO Auto-generated constructor stub 17 | this.sözlük = sözlük; 18 | } 19 | 20 | public boolean isValid() 21 | { 22 | return true; 23 | } 24 | 25 | public static String upuzunBayağıUzunBirMetodYazıyorumŞuAnda() 26 | { 27 | return ""; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item5/YazıAnaliziSingleton.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item5; 2 | 3 | public class YazıAnaliziSingleton { 4 | 5 | private final static Sözlük sözlük = new Sözlük("İngilizce"); 6 | public static YazıAnaliziSingleton INSTANCE = new YazıAnaliziSingleton(); 7 | 8 | private YazıAnaliziSingleton() { 9 | // TODO Auto-generated constructor stub 10 | } 11 | 12 | public boolean isValid() 13 | { 14 | return true; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item5/YazıAnaliziUtility.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item5; 2 | 3 | public class YazıAnaliziUtility { 4 | 5 | private static final Sözlük sözlük = new Sözlük("Türkçe"); 6 | 7 | private YazıAnaliziUtility() { 8 | // TODO Auto-generated constructor stub 9 | } 10 | 11 | public static boolean isValid() 12 | { 13 | // Doldur 14 | return true; 15 | } 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item6/ItemSixTest.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item6; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class ItemSixTest { 7 | 8 | private static long topla() 9 | { 10 | Long sum = 0L; 11 | for(long i = 0; i<10000; i++) 12 | { 13 | sum += i; 14 | } 15 | return sum; 16 | } 17 | 18 | public static void main(String[] args) { 19 | 20 | long startTime = System.nanoTime(); 21 | topla(); 22 | long endTime = System.nanoTime(); 23 | long totalTime = endTime - startTime; 24 | System.out.println(totalTime); 25 | 26 | 27 | 28 | //Integer number = 100; 29 | 30 | ArrayList integer = new ArrayList(); 31 | for (int i = 0; i < 10; i++) { 32 | 33 | integer.add(i); 34 | // i = Integer.valueOf(i); 35 | 36 | } 37 | 38 | List li = new ArrayList<>(); 39 | int sum = 0; 40 | for (Integer i : li) 41 | { 42 | if (i % 2 == 0) // i.intValue() 43 | sum += i; 44 | } 45 | 46 | 47 | 48 | } 49 | 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item6/RegExample.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item6; 2 | 3 | import java.util.regex.Pattern; 4 | 5 | public class RegExample { 6 | 7 | private static final Pattern ROMAN = Pattern.compile("^(?=.)M*(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$"); 8 | 9 | public static boolean isRomanNumeral(String s) 10 | { 11 | return s.matches("^(?=.)M*(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$"); 12 | } 13 | 14 | static boolean isNonNumeral(String s) 15 | { 16 | return ROMAN.matcher(s).matches(); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item7/GarbageCollection.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item7; 2 | 3 | public class GarbageCollection { 4 | 5 | public void finalize() 6 | { 7 | System.out.println("Çöpler temizlendi."); 8 | } 9 | 10 | 11 | public static void main(String[] args) { 12 | 13 | GarbageCollection ali = new GarbageCollection(); 14 | GarbageCollection mehmet = new GarbageCollection(); 15 | GarbageCollection sıla = new GarbageCollection(); 16 | 17 | ali = null; 18 | 19 | sıla = mehmet; 20 | 21 | System.gc(); 22 | 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item7/ItemSevenTest.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item7; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Random; 5 | 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | @RestController 10 | public class ItemSevenTest { 11 | 12 | private static Random random = new Random(); 13 | public static final ArrayList list = new ArrayList(1000000); 14 | 15 | @GetMapping("/hello1") 16 | public static void main(String[] args) throws InterruptedException { 17 | 18 | for (int i = 0; i < 1000000; i++) 19 | { 20 | list.add(random.nextDouble()); 21 | } 22 | 23 | } 24 | 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item7/ItemSevenTest2.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item7; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Random; 5 | 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | @RestController 10 | public class ItemSevenTest2 { 11 | 12 | private static Random random = new Random(); 13 | 14 | @GetMapping("/hello2") 15 | public static void main(String[] args) { 16 | 17 | 18 | ArrayList list = new ArrayList(1000000); 19 | for (int i = 0; i < 1000000; i++) 20 | { 21 | list.add(random.nextDouble()); 22 | } 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item7/ItemSevenTest3.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item7; 2 | 3 | import org.springframework.web.bind.annotation.GetMapping; 4 | import org.springframework.web.bind.annotation.RestController; 5 | 6 | @RestController 7 | public class ItemSevenTest3 { 8 | 9 | @GetMapping("/hello3") 10 | public static void main(String[] args) { 11 | 12 | Stack stack = new Stack(); 13 | 14 | for (int i = 1; i < 10000000; i++) 15 | { 16 | stack.push(i); 17 | } 18 | 19 | for (int i = 1; i < 10000000; i++) 20 | { 21 | stack.popWell(); 22 | } 23 | 24 | System.out.println("hello"); 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item7/Stack.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.item7; 2 | 3 | import java.util.Arrays; 4 | import java.util.EmptyStackException; 5 | 6 | public class Stack { 7 | 8 | public Object[] elements; 9 | private int size = 0; 10 | private static final int DEFAULT_INITIAL_CAPACITY = 16; 11 | 12 | public void finalize() 13 | { 14 | System.out.println("Çöpler Toplandı"); 15 | } 16 | 17 | public Stack() { 18 | // TODO Auto-generated constructor stub 19 | elements = new Object[DEFAULT_INITIAL_CAPACITY]; 20 | } 21 | 22 | public void push(Object e) 23 | { 24 | ensureCapacity(); 25 | elements[size++] = e; 26 | } 27 | 28 | public Object pop() 29 | { 30 | if(size == 0) 31 | throw new EmptyStackException(); 32 | return elements[--size]; 33 | } 34 | 35 | public Object popWell() 36 | { 37 | if(size == 0) 38 | throw new EmptyStackException(); 39 | 40 | Object result = elements[--size]; 41 | elements[size] = null; 42 | 43 | return result; 44 | 45 | } 46 | 47 | /** 48 |      * Ensure space for at least one more element, roughly 49 |      * doubling the capacity each time the array needs to grow. 50 |      */ 51 | private void ensureCapacity() 52 | { 53 | if(elements.length == size) 54 | { 55 | elements = Arrays.copyOf(elements, 2 * size + 1); 56 | } 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/item8a9/test.txt: -------------------------------------------------------------------------------- 1 | Metallica, King Nothing -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema1011/ColorPoint.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema1011; 2 | 3 | import java.awt.Color; 4 | import java.util.Objects; 5 | 6 | //Adds a value component without violating the equals contract (page 44) 7 | public class ColorPoint { 8 | private final Point point; 9 | private final Color color; 10 | 11 | public ColorPoint(int x, int y, Color color) { 12 | point = new Point(x, y); 13 | this.color = Objects.requireNonNull(color); 14 | } 15 | 16 | /** 17 | * Returns the point-view of this color point. 18 | */ 19 | public Point asPoint() { 20 | return point; 21 | } 22 | 23 | @Override 24 | public boolean equals(Object o) { 25 | if (!(o instanceof ColorPoint)) 26 | return false; 27 | ColorPoint cp = (ColorPoint) o; 28 | return cp.point.equals(point) && cp.color.equals(color); 29 | } 30 | 31 | @Override 32 | public int hashCode() { 33 | return 31 * point.hashCode() + color.hashCode(); 34 | } 35 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema1011/DenemeSınıf.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema1011; 2 | 3 | public class DenemeSınıf { 4 | 5 | private int a,b; 6 | 7 | public DenemeSınıf(int a, int b) { 8 | // TODO Auto-generated constructor stub 9 | 10 | this.a = a; 11 | this.b = b; 12 | } 13 | 14 | 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema1011/DenemeSınıfOvrd.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema1011; 2 | 3 | 4 | public class DenemeSınıfOvrd { 5 | 6 | private int a,b; 7 | 8 | public DenemeSınıfOvrd(int a, int b) { 9 | // TODO Auto-generated constructor stub 10 | 11 | this.a = a; 12 | this.b = b; 13 | } 14 | 15 | 16 | // @Override 17 | // public boolean equals(Object o) { 18 | // 19 | // // If the object is compared with itself then return true 20 | // if (o == this) { 21 | // return true; 22 | // } 23 | // 24 | // /* Check if o is an instance of DenemeSınıfOvrd or not 25 | // "null instanceof [type]" also returns false */ 26 | // if (!(o instanceof DenemeSınıfOvrd)) { 27 | // return false; 28 | // } 29 | // 30 | // // typecast o to DenemeSınıfOvrd so that we can compare data members 31 | // DenemeSınıfOvrd c = (DenemeSınıfOvrd) o; 32 | // 33 | // // Compare the data members and return accordingly 34 | // return Integer.compare(a, c.a) == 0 35 | // && Integer.compare(b, c.b) == 0; 36 | // } 37 | 38 | // @Override 39 | // public int hashCode() { 40 | // // TODO Auto-generated method stub 41 | // int result = Integer.hashCode(b); 42 | // result = 31*result + Integer.hashCode(a); 43 | // return result; 44 | // } 45 | 46 | 47 | // @Override 48 | // public int hashCode() { 49 | // return Objects.hash(a, b); 50 | // } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema1011/Point.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema1011; 2 | 3 | public class Point { 4 | private final int x; 5 | private final int y; 6 | 7 | public Point(int x, int y) { 8 | this.x = x; 9 | this.y = y; 10 | } 11 | 12 | @Override 13 | public boolean equals(Object o) { 14 | if (!(o instanceof Point)) 15 | return false; 16 | Point p = (Point) o; 17 | return p.x == x && p.y == y; 18 | } 19 | 20 | // // Broken - violates Liskov substitution principle (page 43) 21 | // @Override public boolean equals(Object o) { 22 | // if (o == null || o.getClass() != getClass()) 23 | // return false; 24 | // Point p = (Point) o; 25 | // return p.x == x && p.y == y; 26 | // } 27 | 28 | // See Item 11 29 | @Override 30 | public int hashCode() { 31 | return 31 * x + y; 32 | } 33 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema12/ItemTwelveTest.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema12; 2 | 3 | import com.effectivejava.tutorial.effectivejava.itema15.GuvenliKod; 4 | 5 | public class ItemTwelveTest extends GuvenliKod{ 6 | 7 | public static void main(String[] args) { 8 | 9 | Request as = new Request(); 10 | as.setBody("Govde"); 11 | as.setHeader("Kafa"); 12 | 13 | System.out.println(as.toString()); 14 | 15 | 16 | 17 | 18 | 19 | } 20 | 21 | @Override 22 | public void mesaj() { 23 | // TODO Auto-generated method stub 24 | 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema12/Request.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema12; 2 | 3 | public class Request { 4 | 5 | public String header; 6 | public String body; 7 | 8 | 9 | public String getHeader() { 10 | return header; 11 | } 12 | public void setHeader(String header) { 13 | this.header = header; 14 | } 15 | public String getBody() { 16 | return body; 17 | } 18 | public void setBody(String body) { 19 | this.body = body; 20 | } 21 | 22 | 23 | 24 | // @Override 25 | // public String toString() { 26 | // return "Request [header=" + header + ", body=" + body + "]"; 27 | // } 28 | 29 | // public String toString() { 30 | // return new com.google.gson.Gson().toJson(this); 31 | // } 32 | 33 | 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema12/Virus.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema12; 2 | 3 | import com.effectivejava.tutorial.effectivejava.itema15.GuvenliKod; 4 | 5 | public class Virus { 6 | 7 | public String virus() 8 | { 9 | String virusUrl = "kötühaberler.com"; 10 | GuvenliKod.ANLASMALI_SITELER[0] = virusUrl; 11 | GuvenliKod.haberleriAl(virusUrl); 12 | 13 | return ""; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema13/ItemThirteenTest.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema13; 2 | 3 | 4 | public class ItemThirteenTest { 5 | 6 | public static void main(String[] args) { 7 | 8 | 9 | // Shallow Copy 10 | Stack st = new Stack(); 11 | Stack st1 = st; 12 | 13 | System.out.println(st == st1); 14 | 15 | 16 | // Deep Copy 17 | Stack stc = new Stack(); 18 | stc.stackName = "furkan"; 19 | 20 | Stack stc2 = new Stack(); 21 | stc2.stackName = stc.stackName; 22 | 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema14/CaseInsensitiveString.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema14; 2 | 3 | import java.util.*; 4 | 5 | //Single-field Comparable with object reference field (Page 69) 6 | public final class CaseInsensitiveString implements Comparable { 7 | private final String s; 8 | 9 | public CaseInsensitiveString(String s) { 10 | this.s = Objects.requireNonNull(s); 11 | } 12 | 13 | // Fixed equals method (Page 40) 14 | @Override 15 | public boolean equals(Object o) { 16 | return o instanceof CaseInsensitiveString && ((CaseInsensitiveString) o).s.equalsIgnoreCase(s); 17 | } 18 | 19 | @Override 20 | public int hashCode() { 21 | return s.hashCode(); 22 | } 23 | 24 | @Override 25 | public String toString() { 26 | return s; 27 | } 28 | 29 | // Using an existing comparator to make a class comparable 30 | public int compareTo(CaseInsensitiveString cis) { 31 | return String.CASE_INSENSITIVE_ORDER.compare(s, cis.s); 32 | } 33 | 34 | /* 35 | @Override 36 | public int compareTo(CaseInsensitiveString o) { 37 | // TODO Auto-generated method stub 38 | return 0; 39 | } 40 | */ 41 | 42 | 43 | public static void main(String[] args) { 44 | Set s = new TreeSet<>(); 45 | for (String arg : args) 46 | s.add(new CaseInsensitiveString(arg)); 47 | System.out.println(s); 48 | } 49 | 50 | 51 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema14/IsımKıyaslama.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema14; 2 | 3 | import java.util.Comparator; 4 | 5 | public class IsımKıyaslama implements Comparator { 6 | 7 | public int compare(Kitap m1, Kitap m2) { 8 | return m1.getIsim().compareTo(m2.getIsim()); 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema14/ItemFourteenTest.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema14; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | 6 | public class ItemFourteenTest { 7 | 8 | public static void main(String[] args) { 9 | 10 | ArrayList kitapListesi = new ArrayList(); 11 | 12 | kitapListesi.add(new Kitap("Şibumi", 500)); 13 | kitapListesi.add(new Kitap("Otomatik Portakal", 200)); 14 | kitapListesi.add(new Kitap("Ab", 200)); 15 | 16 | Collections.sort(kitapListesi); 17 | 18 | System.out.println("Sıralama1"); 19 | for (Kitap kitap : kitapListesi) { 20 | System.out.println(kitap.toString()); 21 | } 22 | 23 | // Sıralama 2 24 | System.out.println("Sıralama2"); 25 | IsımKıyaslama ismeGöre = new IsımKıyaslama(); 26 | Collections.sort(kitapListesi, ismeGöre); 27 | for (Kitap kitap : kitapListesi) { 28 | System.out.println(kitap.toString()); 29 | } 30 | 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema14/Kitap.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema14; 2 | 3 | public class Kitap implements Comparable{ 4 | 5 | private String isim; 6 | private int sayfa; 7 | 8 | public Kitap(String isim, int sayfa) { 9 | // TODO Auto-generated constructor stub 10 | this.isim = isim; 11 | this.sayfa = sayfa; 12 | } 13 | 14 | public String getIsim() { 15 | return isim; 16 | } 17 | 18 | public void setIsim(String isim) { 19 | this.isim = isim; 20 | } 21 | 22 | public int getSayfa() { 23 | return sayfa; 24 | } 25 | 26 | public void setSayfa(int sayfa) { 27 | this.sayfa = sayfa; 28 | } 29 | 30 | @Override 31 | public int compareTo(Kitap o) { 32 | // TODO Auto-generated method stub 33 | return this.sayfa - o.sayfa; 34 | } 35 | 36 | @Override 37 | public String toString() { 38 | return "Kitap [isim=" + isim + ", sayfa=" + sayfa + "]"; 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema15/GuvenliKod.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema15; 2 | 3 | public abstract class GuvenliKod { 4 | 5 | public static final String[] ANLASMALI_SITELER = new String[] { "http://hızlıguvenlitemizhaber.com", 6 | "http://engercekciharikahaberler.com" }; 7 | 8 | // this method allows untrusted code to connect to allowed sites (only) 9 | public static void haberleriAl(String url) { 10 | for (String haberler : ANLASMALI_SITELER) { 11 | if (url.equals(haberler)) { 12 | // Yap bişeyler 13 | } 14 | } 15 | } 16 | 17 | public static final String a = "a"; 18 | 19 | public abstract void mesaj(); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema15/ItemFifthteenTest.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema15; 2 | 3 | public class ItemFifthteenTest { 4 | 5 | public static void main(String[] args) { 6 | 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema16/Futbol.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema16; 2 | 3 | public class Futbol { 4 | 5 | // 1-> Doğrudan erişilebilirliği kes 6 | // 2-> verilerimde kontrol sağlıyorum. 7 | // 3-> Read only 8 | // 4-> write only 9 | 10 | // 5-> Data hiding.. 11 | 12 | 13 | private String takımAdı; 14 | private int takımOyuncuSayısı; 15 | 16 | public Futbol(String takımAdı, int takımOyuncuSayısı) { 17 | super(); 18 | this.takımAdı = takımAdı; 19 | this.takımOyuncuSayısı = takımOyuncuSayısı; 20 | } 21 | 22 | 23 | public void setTakımAdı(String takımAdı) { 24 | this.takımAdı = takımAdı; 25 | } 26 | 27 | public int getTakımOyuncuSayısı() { 28 | return takımOyuncuSayısı; 29 | } 30 | 31 | public void setTakımOyuncuSayısı(int takımOyuncuSayısı) { 32 | if(takımOyuncuSayısı < 0) 33 | { 34 | System.out.println("olmaz"); 35 | } 36 | this.takımOyuncuSayısı = takımOyuncuSayısı; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema16/ItemSixteenTest.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema16; 2 | 3 | public class ItemSixteenTest { 4 | 5 | public static void main(String[] args) { 6 | 7 | Futbol fb = new Futbol("FenerBahce", 35); 8 | 9 | fb.setTakımOyuncuSayısı(-35); 10 | 11 | 12 | Time t = new Time(10, 11); 13 | 14 | 15 | 16 | } 17 | 18 | 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema16/Time.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema16; 2 | 3 | //Public class with exposed immutable fields - questionable (Page 79) 4 | public final class Time { 5 | private static final int HOURS_PER_DAY = 24; 6 | private static final int MINUTES_PER_HOUR = 60; 7 | 8 | public final int hour; 9 | public final int minute; 10 | 11 | public Time(int hour, int minute) { 12 | if (hour < 0 || hour >= HOURS_PER_DAY) 13 | throw new IllegalArgumentException("Hour: " + hour); 14 | if (minute < 0 || minute >= MINUTES_PER_HOUR) 15 | throw new IllegalArgumentException("Min: " + minute); 16 | this.hour = hour; 17 | this.minute = minute; 18 | } 19 | 20 | // Remainder omitted 21 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema17/Immutable.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema17; 2 | 3 | public class Immutable { 4 | 5 | private final int value; 6 | 7 | public Immutable(int value) { 8 | this.value = value; 9 | } 10 | 11 | public int getValue() { 12 | return value; 13 | } 14 | // read only 15 | 16 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema17/Mutable.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema17; 2 | 3 | public class Mutable extends Immutable { 4 | private int realValue; 5 | 6 | public Mutable(int value) { 7 | super(value); 8 | 9 | realValue = value; 10 | } 11 | 12 | public int getValue() { 13 | return realValue; 14 | } 15 | 16 | public void setValue(int newValue) { 17 | realValue = newValue; 18 | } 19 | 20 | public static void main(String[] arg) { 21 | 22 | Mutable obj = new Mutable(4); 23 | Immutable immObj = (Immutable) obj; 24 | 25 | System.out.println(immObj.getValue()); 26 | 27 | obj.setValue(8); 28 | System.out.println(immObj.getValue()); 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema18/Hayvan.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema18; 2 | 3 | public class Hayvan { 4 | 5 | String hayvanİsmi = "Hayvan"; 6 | 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema18/Kedi.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema18; 2 | 3 | public class Kedi extends Hayvan{ 4 | 5 | String hayvanTipi = "Kedi"; 6 | 7 | public static void main(String[] args) { 8 | 9 | Kedi kedi = new Kedi(); 10 | 11 | 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema18/Kitap.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema18; 2 | 3 | public class Kitap { 4 | 5 | private String isim; 6 | 7 | public Kitap(String isim) { 8 | // TODO Auto-generated constructor stub 9 | this.isim = isim; 10 | } 11 | 12 | public String getIsim() { 13 | return isim; 14 | } 15 | 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema18/KompozisyonDüzeltmesi.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema18; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | import com.effectivejava.tutorial.effectivejava.itema1011.Point; 7 | 8 | public class KompozisyonDüzeltmesi { 9 | 10 | private static Set set = new HashSet<>(); 11 | 12 | public static void main(String[] args) { 13 | 14 | set.add("Kötü"); 15 | set.forEach(System.out::println); 16 | 17 | 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema18/KötüBirKalıtımÖrneği.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema18; 2 | 3 | import java.util.HashSet; 4 | 5 | public class KötüBirKalıtımÖrneği extends HashSet{ 6 | 7 | 8 | public static void main(String[] args) { 9 | 10 | KötüBirKalıtımÖrneği kötüÖrnek = new KötüBirKalıtımÖrneği(); 11 | 12 | kötüÖrnek.add("kötü"); 13 | kötüÖrnek.forEach(System.out::println); 14 | 15 | 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema18/Kütüphane.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema18; 2 | 3 | import java.util.List; 4 | 5 | public class Kütüphane { 6 | 7 | private final List kitaplar; 8 | 9 | public Kütüphane(List kitaplar) { 10 | // TODO Auto-generated constructor stub 11 | this.kitaplar = kitaplar; 12 | } 13 | 14 | public List kütüphanedekiTümKitaplar() { 15 | return kitaplar; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema18/TestDrive.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema18; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class TestDrive { 7 | 8 | public static void main(String[] args) { 9 | 10 | 11 | KötüBirKalıtımÖrneği kötü = new KötüBirKalıtımÖrneği(); 12 | 13 | 14 | 15 | Kitap kitap1 = new Kitap("Amerikan Sapığı"); 16 | Kitap kitap2 = new Kitap("Körlük"); 17 | 18 | List kitapListesi = new ArrayList(); 19 | 20 | kitapListesi.add(kitap1); 21 | kitapListesi.add(kitap2); 22 | 23 | Kütüphane kütüphane = new Kütüphane(kitapListesi); 24 | 25 | List kütüphanedekiKitaplar = kütüphane.kütüphanedekiTümKitaplar(); 26 | 27 | for (Kitap kitap : kütüphanedekiKitaplar) 28 | { 29 | System.out.println(kitap.getIsim()); 30 | } 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema19/AnaSınıf.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema19; 2 | 3 | abstract class AnaSınıf { 4 | 5 | public AnaSınıf() { 6 | overrideMe(); 7 | } 8 | 9 | abstract void overrideMe(); 10 | 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema19/CocukSınıf.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema19; 2 | 3 | public class CocukSınıf extends AnaSınıf{ 4 | 5 | final int deger; 6 | 7 | public CocukSınıf(int deger) { 8 | this.deger = deger; 9 | } 10 | 11 | @Override 12 | void overrideMe() { 13 | // TODO Auto-generated method stub 14 | System.out.println(deger); 15 | } 16 | 17 | 18 | public static void main(String[] args) { 19 | 20 | CocukSınıf cocuk = new CocukSınıf(42); 21 | 22 | 23 | 24 | 25 | } 26 | 27 | 28 | 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema19/KalıtımKaçış.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema19; 2 | 3 | 4 | 5 | //Do not inherit this class -- bu sınıfı extends 6 | public class KalıtımKaçış { 7 | 8 | 9 | 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema19/KalıtımÇocuk.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema19; 2 | 3 | public class KalıtımÇocuk extends KalıtımKaçış{ 4 | 5 | /** 6 | * 7 | */ 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema19/Sub.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema19; 2 | 3 | import java.time.Instant; 4 | 5 | //Demonstration of what can go wrong when you override a method called from constructor (Page 96) 6 | public final class Sub extends Super { 7 | // Blank final, set by constructor 8 | private final Instant instant; 9 | 10 | Sub() { 11 | instant = Instant.now(); 12 | } 13 | 14 | // Overriding method invoked by superclass constructor 15 | @Override 16 | public void overrideMe() { 17 | System.out.println(instant); 18 | } 19 | 20 | 21 | public static void main(String[] args) { 22 | Sub sub = new Sub(); 23 | sub.overrideMe(); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema19/Super.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema19; 2 | 3 | //Class whose constructor invokes an overridable method. NEVER DO THIS! (Page 95) 4 | public class Super { 5 | // Broken - constructor invokes an overridable method 6 | public Super() { 7 | overrideMe(); 8 | } 9 | 10 | 11 | public void overrideMe() { 12 | 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema19/TestDrive.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema19; 2 | 3 | public class TestDrive { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema20/AbstractMapEntry.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema20; 2 | 3 | import java.util.*; 4 | 5 | //Skeletal implementation class (Pages 102-3) 6 | public abstract class AbstractMapEntry implements Map.Entry { 7 | // Entries in a modifiable map must override this method 8 | @Override 9 | public V setValue(V value) { 10 | throw new UnsupportedOperationException(); 11 | } 12 | 13 | // Implements the general contract of Map.Entry.equals 14 | @Override 15 | public boolean equals(Object o) { 16 | if (o == this) 17 | return true; 18 | if (!(o instanceof Map.Entry)) 19 | return false; 20 | Map.Entry e = (Map.Entry) o; 21 | return Objects.equals(e.getKey(), getKey()) && Objects.equals(e.getValue(), getValue()); 22 | } 23 | 24 | // Implements the general contract of Map.Entry.hashCode 25 | @Override 26 | public int hashCode() { 27 | return Objects.hashCode(getKey()) ^ Objects.hashCode(getValue()); 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | return getKey() + "=" + getValue(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema20/Hayvan.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema20; 2 | 3 | public abstract class Hayvan { 4 | 5 | abstract void sesÇıkar(); 6 | 7 | public void nefesAl() 8 | { 9 | System.out.println("Nefes alıyorum."); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema20/IntArrays.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema20; 2 | 3 | import java.util.*; 4 | 5 | //Concrete implementation built atop skeletal implementation (Page 101) 6 | public class IntArrays { 7 | static List intArrayAsList(int[] a) { 8 | Objects.requireNonNull(a); 9 | 10 | // The diamond operator is only legal here in Java 9 and later 11 | // If you're using an earlier release, specify 12 | return new AbstractList () { 13 | @Override 14 | public Integer get(int i) { 15 | return a[i]; // Autoboxing (Item 6) 16 | } 17 | 18 | @Override 19 | public Integer set(int i, Integer val) { 20 | int oldVal = a[i]; 21 | a[i] = val; // Auto-unboxing 22 | return oldVal; // Autoboxing 23 | } 24 | 25 | @Override 26 | public int size() { 27 | return a.length; 28 | } 29 | }; 30 | } 31 | 32 | public static void main(String[] args) { 33 | int[] a = new int[10]; 34 | for (int i = 0; i < a.length; i++) 35 | a[i] = i; 36 | 37 | List list = intArrayAsList(a); 38 | Collections.shuffle(list); 39 | System.out.println(list); 40 | } 41 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema20/Kuş.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema20; 2 | 3 | public class Kuş extends Hayvan { 4 | 5 | @Override 6 | void sesÇıkar() { 7 | // TODO Auto-generated method stub 8 | System.out.println("cik cik"); 9 | } 10 | 11 | 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema20/TestDrive.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema20; 2 | 3 | import java.util.Collection; 4 | 5 | public class TestDrive implements ŞarkıcıŞarkıYazarı{ 6 | 7 | @Override 8 | public void şarkıSöyle() { 9 | // TODO Auto-generated method stub 10 | 11 | } 12 | 13 | @Override 14 | public void şarkıYaz() { 15 | // TODO Auto-generated method stub 16 | 17 | } 18 | 19 | @Override 20 | public void çalBirşeyler() { 21 | // TODO Auto-generated method stub 22 | 23 | } 24 | 25 | public static void main(String[] args) { 26 | 27 | TestDrive drive = new TestDrive(); 28 | 29 | 30 | } 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema20/ŞarkıYazarı.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema20; 2 | 3 | public interface ŞarkıYazarı { 4 | 5 | public void şarkıYaz(); 6 | 7 | default boolean şiirYazabiliyormusun() 8 | { 9 | 10 | return true; 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema20/Şarkıcı.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema20; 2 | 3 | public interface Şarkıcı { 4 | 5 | public void şarkıSöyle(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema20/ŞarkıcıŞarkıYazarı.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema20; 2 | 3 | public interface ŞarkıcıŞarkıYazarı extends Şarkıcı, ŞarkıYazarı { 4 | 5 | public void çalBirşeyler(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema21/TestDrive.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema21; 2 | 3 | public class TestDrive { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema22/DenemeSınıf.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema22; 2 | 3 | public class DenemeSınıf implements PhysicalConstants{ 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema22/Kus.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema22; 2 | 3 | public class Kus implements UcmaYetisi{ 4 | 5 | 6 | public void cikSesi() 7 | { 8 | System.out.println("cik"); 9 | } 10 | 11 | @Override 12 | public boolean ucuyorum() { 13 | // TODO Auto-generated method stub 14 | return true; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema22/PhysicalConstant.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema22; 2 | 3 | //Constant utility class (Page 108) 4 | public class PhysicalConstant { 5 | 6 | private PhysicalConstant() { 7 | } // Prevents instantiation 8 | 9 | // Avogadro's number (1/mol) 10 | public static final double AVOGADROS_NUMBER = 6.022_140_857e23; 11 | 12 | // Boltzmann constant (J/K) 13 | public static final double BOLTZMANN_CONST = 1.380_648_52e-23; 14 | 15 | // Mass of the electron (kg) 16 | public static final double ELECTRON_MASS = 9.109_383_56e-31; 17 | 18 | 19 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema22/PhysicalConstants.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema22; 2 | 3 | //Constant interface antipattern - do not use! 4 | public interface PhysicalConstants { 5 | 6 | static final double doubleDeger = 3.4; 7 | 8 | // Avogadro's number (1/mol) 9 | static final double AVOGADROS_NUMBER = 6.022_140_857e23; 10 | 11 | // Boltzmann constant (J/K) 12 | static final double BOLTZMANN_CONSTANT = 1.380_648_52e-23; 13 | 14 | // Mass of the electron (kg) 15 | static final double ELECTRON_MASS = 9.109_383_56e-31; 16 | 17 | 18 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema22/TestDrive.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema22; 2 | 3 | 4 | public class TestDrive { 5 | 6 | // java.io.ObjectStreamConstants 7 | 8 | public static void main(String[] args) { 9 | 10 | DenemeSınıf ds = new DenemeSınıf(); 11 | 12 | UcmaYetisi uy = new Kus(); 13 | uy.ucuyorum(); 14 | 15 | Kus ks = new Kus(); 16 | ks.cikSesi(); 17 | 18 | 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema22/UcmaYetisi.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema22; 2 | 3 | public interface UcmaYetisi { 4 | 5 | public boolean ucuyorum(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema23/Circle.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema23; 2 | 3 | //Class hierarchy replacement for a tagged class (Page 110-11) 4 | class Circle extends FigureTrue { 5 | 6 | final double radius; 7 | 8 | Circle(double radius) { 9 | this.radius = radius; 10 | } 11 | 12 | @Override 13 | double area() { 14 | return Math.PI * (radius * radius); 15 | } 16 | 17 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema23/Figure.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema23; 2 | 3 | //Tagged class - vastly inferior to a class hierarchy! (Page 109) 4 | class Figure { 5 | 6 | enum Shape { 7 | RECTANGLE, CIRCLE 8 | }; 9 | 10 | // Tag field - the shape of this figure 11 | final Shape shape; 12 | 13 | // These fields are used only if shape is RECTANGLE 14 | double length; 15 | double width; 16 | 17 | // This field is used only if shape is CIRCLE 18 | double radius; 19 | 20 | // Constructor for circle 21 | Figure(double radius) { 22 | shape = Shape.CIRCLE; 23 | this.radius = radius; 24 | } 25 | 26 | // Constructor for rectangle 27 | Figure(double length, double width) { 28 | shape = Shape.RECTANGLE; 29 | this.length = length; 30 | this.width = width; 31 | } 32 | 33 | double area() { 34 | switch (shape) { 35 | case RECTANGLE: 36 | return length * width; 37 | case CIRCLE: 38 | return Math.PI * (radius * radius); 39 | default: 40 | throw new AssertionError(shape); 41 | } 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema23/FigureTrue.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema23; 2 | 3 | //Class hierarchy replacement for a tagged class (Page 110-11) 4 | abstract class FigureTrue { 5 | 6 | abstract double area(); 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema23/Rectangle.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema23; 2 | 3 | //Class hierarchy replacement for a tagged class (Page 110-11) 4 | class Rectangle extends FigureTrue { 5 | 6 | final double length; 7 | final double width; 8 | 9 | Rectangle(double length, double width) { 10 | this.length = length; 11 | this.width = width; 12 | } 13 | 14 | @Override 15 | double area() { 16 | return length * width; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema23/Square.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema23; 2 | 3 | //Class hierarchy replacement for a tagged class (Page 110-11) 4 | class Square extends Rectangle { 5 | 6 | Square(double side) { 7 | super(side, side); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema23/TestDrive.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema23; 2 | 3 | public class TestDrive { 4 | 5 | 6 | public static void main(String[] args) { 7 | 8 | Square sq = new Square(2.2); 9 | Figure fg = new Figure(4.4); 10 | 11 | 12 | 13 | } 14 | 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema24/LocalClass.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema24; 2 | 3 | public class LocalClass { 4 | 5 | public void yazBirşeyler() 6 | { 7 | 8 | class localClassInner 9 | { 10 | 11 | 12 | } 13 | 14 | localClassInner localInner = new localClassInner(); 15 | 16 | } 17 | 18 | 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema24/NonStaticNested.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema24; 2 | 3 | public class NonStaticNested { 4 | 5 | private String sözcük = "Sözcük"; 6 | 7 | public class NonStaticNestedClass 8 | { 9 | 10 | public void yazBunu() 11 | { 12 | System.out.println(sözcük); 13 | } 14 | 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema24/StaticNested.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema24; 2 | 3 | public class StaticNested { 4 | 5 | public static class staticNestedClass{ 6 | 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema24/SuperClass.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema24; 2 | 3 | public class SuperClass { 4 | 5 | public void yazBeni() 6 | { 7 | System.out.println("SuperSınıf Yazdır metodu çalıştı"); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema24/SuperClassDerin.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema24; 2 | 3 | @FunctionalInterface 4 | public interface SuperClassDerin { 5 | 6 | public void yazBeni(); 7 | 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema24/TestDrive.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema24; 2 | 3 | import java.util.AbstractSet; 4 | 5 | public class TestDrive { 6 | 7 | public static void main(String[] args) { 8 | 9 | 10 | /* 11 | * statik olan iç içe geçmiş sınıflar (static nested classes) 12 | statik olmayan iç içe geçmiş sınıflar (non-static nested classes) 13 | yerel sınıflar (local classes) 14 | anonim sınıflar (anonymous classes) 15 | * 16 | */ 17 | 18 | 19 | // // Static Nested 20 | // StaticNested.staticNestedClass nesne = new StaticNested.staticNestedClass(); 21 | // // HesapMakinesi.Operasyon.TOPLA 22 | // // Builder Ders2 23 | // 24 | // 25 | // // Non Static Nested 26 | // // Open for a memory leak.. 27 | // 28 | // NonStaticNested nonStatic = new NonStaticNested(); 29 | // NonStaticNested.NonStaticNestedClass nesne2 = nonStatic.new NonStaticNestedClass(); 30 | // nesne2.yazBunu(); 31 | 32 | // Local classes 33 | LocalClass lc = new LocalClass(); 34 | 35 | 36 | // Anonim 37 | SuperClass sc = new SuperClass() { 38 | @Override 39 | public void yazBeni() { 40 | System.out.println("anonim"); 41 | } 42 | }; 43 | 44 | sc.yazBeni(); 45 | 46 | 47 | 48 | SuperClassDerin scd = new SuperClassDerin() { 49 | @Override 50 | public void yazBeni() { 51 | // TODO Auto-generated method stub 52 | 53 | } 54 | }; 55 | scd.yazBeni(); 56 | 57 | 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema25/Dessert.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema25; 2 | 3 | //Two classes defined in one file. Don't ever do this! (Page 115) 4 | //class Utensil { 5 | // static final String NAME = "pot"; 6 | //} 7 | // 8 | //class Dessert { 9 | // static final String NAME = "pie"; 10 | //} -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema25/Test.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema25; 2 | 3 | //Static member classes instead of multiple top-level classes (Page 116) 4 | public class Test { 5 | public static void main(String[] args) { 6 | System.out.println(Utensil.NAME + Dessert.NAME); 7 | } 8 | 9 | public void yazdir() 10 | { 11 | System.out.println(Utensil.NAME + Dessert.NAME); 12 | } 13 | 14 | private static class Utensil { 15 | static final String NAME = "pan"; 16 | } 17 | 18 | private static class Dessert { 19 | static final String NAME = "cake"; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema25/TestDrive.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema25; 2 | 3 | public class TestDrive { 4 | 5 | public static void main(String[] args) 6 | { 7 | System.out.println(Utensil.NAME + Dessert.NAME); 8 | 9 | Test t = new Test(); 10 | t.yazdir(); 11 | 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema25/Utensil.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema25; 2 | 3 | //Two classes defined in one file. Don't ever do this! (Page 115) 4 | class Utensil { 5 | static final String NAME = "pan"; 6 | } 7 | 8 | class Dessert { 9 | static final String NAME = "cake"; 10 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema26/Raw.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema26; 2 | 3 | import java.util.*; 4 | 5 | //Fails at runtime - unsafeAdd method uses a raw type (List)! (Page 119) 6 | public class Raw { 7 | 8 | public static void main(String[] args) 9 | { 10 | 11 | List strings = new ArrayList<>(); 12 | unsafeAdd(strings, Integer.valueOf(42)); 13 | String s = strings.get(0); // Has compiler-generated cast 14 | } 15 | 16 | private static void unsafeAdd(List list, Object o) 17 | { 18 | list.add(o); 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema26/TestDrive.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema26; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class TestDrive { 7 | 8 | public static void main(String[] args) { 9 | 10 | List isimListesi = new ArrayList<>(); 11 | isimListesi.add("Furkan"); 12 | isimListesi.add("James"); 13 | 14 | 15 | String isimAl = isimListesi.get(1); 16 | 17 | System.out.println(isimAl); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema27/TestDrive.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema27; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | public class TestDrive { 7 | 8 | public static void main(String[] args) { 9 | 10 | 11 | 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema28/TestDrive.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema28; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class TestDrive { 7 | 8 | 9 | public static void main(String[] args) { 10 | 11 | 12 | // Object[] objectArray = new Long[1]; 13 | // objectArray[0] = true; 14 | 15 | 16 | // List ol = new ArrayList(); // Uyumsuz türler. 17 | // ol.add(1L); 18 | 19 | 20 | /** 21 | * Dizilerde Object[] -> Long[] 'un süper sınıfıdır. 22 | * Ancak List -> List arasında böyle bir bağlantı söz konusu olamaz. Generic bizi kurtarıyor! 23 | * 24 | * Genericleri arraylere tercih etmek bizi kurtarır. 25 | */ 26 | 27 | 28 | // arrayler -> run time da tür güvenliği ancak derlemede sağlamaz 29 | // genericlerde bunun tam tersi geçerli. 30 | // Listleri tercih etmeliyiz. 31 | 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema29/EmptyStackException.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema29; 2 | 3 | public class EmptyStackException extends RuntimeException { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema29/StackTechTwo.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema29; 2 | 3 | import java.util.Arrays; 4 | import java.util.EmptyStackException; 5 | 6 | //Generic stack using Object[] (Pages 130-3) 7 | public class StackTechTwo { 8 | 9 | private Object[] elements; 10 | private int size = 0; 11 | private static final int DEFAULT_INITIAL_CAPACITY = 16; 12 | 13 | public StackTechTwo() { 14 | elements = new Object[DEFAULT_INITIAL_CAPACITY]; 15 | } 16 | 17 | public void push(E e) { 18 | ensureCapacity(); 19 | elements[size++] = e; 20 | } 21 | 22 | // Appropriate suppression of unchecked warning 23 | public E pop() { 24 | if (size == 0) 25 | throw new EmptyStackException(); 26 | 27 | // push requires elements to be of type E, so cast is correct 28 | @SuppressWarnings("unchecked") 29 | E result = (E) elements[--size]; 30 | 31 | elements[size] = null; // Eliminate obsolete reference 32 | return result; 33 | } 34 | 35 | public boolean isEmpty() { 36 | return size == 0; 37 | } 38 | 39 | private void ensureCapacity() { 40 | if (elements.length == size) 41 | elements = Arrays.copyOf(elements, 2 * size + 1); 42 | } 43 | 44 | // Little program to exercise our generic Stack 45 | public static void main(String[] args) { 46 | 47 | StackTechTwo stack = new StackTechTwo<>(); 48 | 49 | for (String arg : args) 50 | stack.push(arg); 51 | 52 | while (!stack.isEmpty()) 53 | System.out.println(stack.pop().toUpperCase()); 54 | 55 | 56 | } 57 | 58 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema29/TestDrive.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema29; 2 | 3 | import java.util.concurrent.DelayQueue; 4 | 5 | public class TestDrive { 6 | 7 | public static void main(String[] args) { 8 | 9 | 10 | 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema30/GenericSingletonFactory.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema30; 2 | 3 | import java.util.function.UnaryOperator; 4 | 5 | //Generic singleton factory pattern (Page 136-7) 6 | public class GenericSingletonFactory { 7 | 8 | // Generic singleton factory pattern 9 | private static UnaryOperator IDENTITY_FN = (t) -> t; 10 | 11 | @SuppressWarnings("unchecked") 12 | public static UnaryOperator identityFunction() { 13 | return (UnaryOperator) IDENTITY_FN; 14 | } 15 | 16 | // Sample program to exercise generic singleton 17 | public static void main(String[] args) { 18 | 19 | String[] strings = { "jute", "hemp", "nylon" }; 20 | UnaryOperator sameString = identityFunction(); 21 | for (String s : strings) 22 | System.out.println(sameString.apply(s)); 23 | 24 | Number[] numbers = { 1, 2.0, 3L }; 25 | UnaryOperator sameNumber = identityFunction(); 26 | for (Number n : numbers) 27 | System.out.println(sameNumber.apply(n)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema30/TestDrive.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema30; 2 | 3 | import java.util.Collection; 4 | import java.util.Collections; 5 | 6 | public class TestDrive { 7 | 8 | public static void main(String[] args) { 9 | 10 | Collections.sort(null); 11 | 12 | // Değiştirilemez ama bir çok türle kullanılır! 13 | // reverseOrder 14 | Collections.emptySet(); 15 | 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema30/Union.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema30; 2 | 3 | import java.util.*; 4 | 5 | //Generic union method and program to exercise it (Pages 135-6) 6 | public class Union { 7 | 8 | // Generic method 9 | public static Set union(Set s1, Set s2) { 10 | 11 | Set result = new HashSet<>(s1); 12 | 13 | result.addAll(s2); 14 | 15 | return result; 16 | } 17 | 18 | 19 | // access modifier dönüşTipi method_name(parameters..) 20 | 21 | 22 | public static int topla(T değer1, T değer2) 23 | { 24 | return değer1.intValue() + değer2.intValue(); 25 | } 26 | 27 | 28 | 29 | // Simple program to exercise generic method 30 | public static void main(String[] args) { 31 | 32 | Set fenerbahce = Set.of("Uğur Boral", "Caner", "Pelkas"); 33 | Set Türkiye = Set.of("Uğur Boral", "Hasan Şaş", "Burak", "Rüştü"); 34 | 35 | 36 | 37 | Set birleşim = union(fenerbahce, Türkiye); 38 | 39 | System.out.println(birleşim); 40 | 41 | 42 | topla(44.4, 12); 43 | 44 | 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema31/A.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema31; 2 | 3 | class A { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema31/B.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema31; 2 | 3 | class B extends A{ 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema31/C.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema31; 2 | 3 | class C extends B{ 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema31/Chooser.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema31; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collection; 5 | import java.util.List; 6 | import java.util.Random; 7 | 8 | // Wildcard type for parameter that serves as an T producer (page 141) 9 | public class Chooser { 10 | private final List choiceList; 11 | private final Random rnd = new Random(); 12 | 13 | public Chooser(Collection choices) { 14 | choiceList = new ArrayList<>(choices); 15 | } 16 | 17 | public T choose() { 18 | return choiceList.get(rnd.nextInt(choiceList.size())); 19 | } 20 | 21 | public static void main(String[] args) { 22 | List intList = List.of(1, 2, 3, 4, 5, 6); 23 | Chooser chooser = new Chooser<>(intList); 24 | for (int i = 0; i < 10; i++) { 25 | Number choice = chooser.choose(); 26 | System.out.println(choice); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema31/D.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema31; 2 | 3 | class D extends B{ 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema31/RecursiveTypeBound.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema31; 2 | 3 | import java.util.*; 4 | 5 | //Using a recursive type bound with wildcards (Page 143) 6 | public class RecursiveTypeBound { 7 | public static > E max(List list) { 8 | if (list.isEmpty()) 9 | throw new IllegalArgumentException("Empty list"); 10 | 11 | E result = null; 12 | for (E e : list) 13 | if (result == null || e.compareTo(result) > 0) 14 | result = e; 15 | 16 | return result; 17 | } 18 | 19 | public static void main(String[] args) { 20 | List argList = Arrays.asList(args); 21 | System.out.println(max(argList)); 22 | } 23 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema31/Swap.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema31; 2 | 3 | import java.util.*; 4 | 5 | //Private helper method for wildcard capture (Page 145) 6 | public class Swap { 7 | public static void swap(List list, int i, int j) { 8 | swapHelper(list, i, j); 9 | } 10 | 11 | // Private helper method for wildcard capture 12 | private static void swapHelper(List list, int i, int j) { 13 | list.set(i, list.set(j, list.get(i))); 14 | } 15 | 16 | public static void main(String[] args) { 17 | // Swap the first and last argument and print the resulting list 18 | List argList = Arrays.asList(args); 19 | swap(argList, 0, argList.size() - 1); 20 | System.out.println(argList); 21 | } 22 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema31/TestDrive.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema31; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collection; 5 | import java.util.Collections; 6 | import java.util.List; 7 | 8 | public class TestDrive { 9 | 10 | 11 | public static void main(String[] args) { 12 | 13 | // PECS 14 | // Producer extend - Consumer super 15 | 16 | List obje1 = new ArrayList(); 17 | 18 | 19 | 20 | List list; 21 | list = new ArrayList(); 22 | 23 | 24 | List list2; 25 | list2 = new ArrayList(); 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema31/Union.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema31; 2 | 3 | import java.util.*; 4 | 5 | //Generic union method with wildcard types for enhanced flexibility (Pages 142-3) 6 | public class Union { 7 | 8 | public static Set union(Set s1, Set s2) { 9 | Set result = new HashSet(s1); 10 | result.addAll(s2); 11 | return result; 12 | } 13 | 14 | 15 | // Simple program to exercise flexible generic staticfactory 16 | public static void main(String[] args) { 17 | 18 | Set integers = new HashSet<>(); 19 | integers.add(1); 20 | integers.add(3); 21 | integers.add(5); 22 | 23 | Set doubles = new HashSet<>(); 24 | doubles.add(2.0); 25 | doubles.add(4.0); 26 | doubles.add(6.0); 27 | 28 | 29 | Set numbers = union(integers, doubles); 30 | 31 | // // Explicit type parameter - required prior to Java 8 32 | // Set numbers = Union.union(integers, doubles); 33 | 34 | System.out.println(numbers); 35 | } 36 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema32/Dangerous.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema32; 2 | 3 | import java.util.List; 4 | 5 | //It is unsafe to store a value in a generic varargs array parameter (Page 146) 6 | public class Dangerous { 7 | 8 | // Mixing generics and varargs can violate type safety! 9 | static void dangerous(List... stringLists) 10 | { 11 | List intList = List.of(42); 12 | Object[] objects = stringLists; 13 | objects[0] = intList; // Heap pollution 14 | 15 | String s = stringLists[0].get(0); // ClassCastException 16 | } 17 | 18 | 19 | public static void main(String[] args) 20 | { 21 | dangerous(List.of("There be dragons!")); 22 | 23 | 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema32/FlattenWithList.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema32; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | // List as a typesafe alternative to a generic varargs parameter (page 149) 7 | public class FlattenWithList { 8 | 9 | static List flatten(List> lists) 10 | { 11 | List result = new ArrayList<>(); 12 | for (List list : lists) 13 | result.addAll(list); 14 | return result; 15 | } 16 | 17 | public static void main(String[] args) 18 | { 19 | List flatList = flatten(List.of(List.of(1, 2), List.of(3, 4, 5), List.of(6, 7))); 20 | System.out.println(flatList); 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema32/FlattenWithVarargs.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema32; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | // Safe method with a generic varargs parameter (page 149) 7 | public class FlattenWithVarargs { 8 | 9 | 10 | @SafeVarargs 11 | static List flatten(List... lists) 12 | { 13 | List result = new ArrayList<>(); 14 | for (List list : lists) 15 | result.addAll(list); 16 | return result; 17 | } 18 | 19 | public static void main(String[] args) 20 | { 21 | List flatList = flatten(List.of(1, 2), List.of(3, 4, 5), List.of(6, 7)); 22 | System.out.println(flatList); 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema32/PickTwo.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema32; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | import java.util.concurrent.ThreadLocalRandom; 7 | 8 | // Subtle heap pollution (Pages 147-8) 9 | public class PickTwo { 10 | 11 | // UNSAFE - Exposes a reference to its generic parameter array! 12 | static T[] toArray(T... args) 13 | { 14 | return args; 15 | } 16 | 17 | static T[] pickTwo(T a, T b, T c) 18 | { 19 | switch (ThreadLocalRandom.current().nextInt(3)) 20 | { 21 | case 0: 22 | return toArray(a, b); 23 | case 1: 24 | return toArray(a, c); 25 | case 2: 26 | return toArray(b, c); 27 | } 28 | throw new AssertionError(); // Can't get here 29 | } 30 | 31 | public static void main(String[] args) { 32 | String[] attributes = pickTwo("Good", "Fast", "Cheap"); 33 | System.out.println(Arrays.toString(attributes)); 34 | 35 | 36 | 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema32/SafePickTwo.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema32; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | import java.util.concurrent.ThreadLocalRandom; 6 | 7 | // Safe version of PickTwo using lists instead of arrays (Page 150) 8 | public class SafePickTwo { 9 | 10 | static List pickTwo(T a, T b, T c) 11 | { 12 | switch (ThreadLocalRandom.current().nextInt(3)) 13 | { 14 | case 0: 15 | return List.of(a, b); 16 | case 1: 17 | return List.of(a, c); 18 | case 2: 19 | return List.of(b, c); 20 | } 21 | throw new AssertionError(); 22 | } 23 | 24 | public static void main(String[] args) 25 | { 26 | List attributes = pickTwo("Good", "Fast", "Cheap"); 27 | System.out.println(attributes); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema32/TestDrive.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema32; 2 | 3 | public class TestDrive { 4 | 5 | 6 | static void sumTest(int ...v) 7 | { 8 | System.out.println(v.length); 9 | System.out.println(v[1]); 10 | } 11 | 12 | 13 | 14 | public static void main(String[] args) 15 | { 16 | 17 | //sumTest(); 18 | //sumTest(1); 19 | sumTest(3,2,3); 20 | 21 | 22 | } 23 | 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema33/Column.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema33; 2 | 3 | public class Column { 4 | 5 | private final Class type; 6 | 7 | public Column(Class type) 8 | { 9 | this.type = type; 10 | } 11 | 12 | public T cast(Object obj) 13 | { 14 | return obj == null ? null : type.cast(obj); 15 | } 16 | 17 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema33/DatabaseClient.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema33; 2 | 3 | public class DatabaseClient { 4 | 5 | // Client 6 | public static void main(String[] args) 7 | { 8 | Column integerColumn = new Column(Integer.class); 9 | Column stringColumn = new Column(String.class); 10 | Column stringColumn2 = new Column(String.class); 11 | 12 | DatabaseRow databaseRow = new DatabaseRow(); 13 | databaseRow.putColumn(integerColumn, 3); 14 | databaseRow.putColumn(stringColumn, "3"); 15 | databaseRow.putColumn(stringColumn2, "4"); 16 | 17 | System.out.println("The integer + 1: " + (databaseRow.getColumn(integerColumn) + 1)); 18 | System.out.println("The string: " + databaseRow.getColumn(stringColumn)); 19 | System.out.println("The String2: " + databaseRow.getColumn(stringColumn2)); 20 | 21 | } 22 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema33/DatabaseRow.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema33; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class DatabaseRow { 7 | 8 | private Map, Object> columns = new HashMap, Object>(); 9 | 10 | public void putColumn(Column type, T instance) 11 | { 12 | if (type == null) 13 | throw new NullPointerException("Type is null"); 14 | 15 | // Achieving runtime type safety with a dynamic cast 16 | // Same trick can be found in the following implementations 17 | // checkedSet, checkedList, checkedMap, and so forth. 18 | columns.put(type, instance.getClass().cast(instance)); 19 | 20 | } 21 | 22 | public T getColumn(Column type) { 23 | 24 | return type.cast(columns.get(type)); 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema33/Favorites.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema33; 2 | 3 | import java.util.*; 4 | 5 | //Typesafe heterogeneous container pattern (Pages 151-4) 6 | public class Favorites implements FavoritesContract { 7 | 8 | private Map, Object> favorites = new HashMap<>(); 9 | 10 | public void putFavorite(Class type, T instance) 11 | { 12 | favorites.put(Objects.requireNonNull(type), instance); 13 | } 14 | 15 | public T getFavorite(Class type) 16 | { 17 | return type.cast(favorites.get(type)); 18 | } 19 | 20 | // Achieving runtime type safety with a dynamic cast 21 | // public void putFavorite(Class type, T instance) 22 | // { 23 | // favorites.put(Objects.requireNonNull(type), type.cast(instance)); 24 | // } 25 | 26 | public void herHangi() 27 | { 28 | 29 | } 30 | 31 | // Client 32 | public static void main(String[] args) 33 | { 34 | 35 | FavoritesContract f = new Favorites(); 36 | 37 | f.putFavorite(String.class, "Java"); 38 | f.putFavorite(Integer.class, 0xcafebabe); 39 | f.putFavorite(Class.class, Favorites.class); 40 | 41 | String favoriteString = f.getFavorite(String.class); 42 | int favoriteInteger = f.getFavorite(Integer.class); 43 | Class favoriteClass = f.getFavorite(Class.class); 44 | 45 | System.out.printf("%s %x %s%n", favoriteString, favoriteInteger, favoriteClass.getName()); 46 | } 47 | 48 | 49 | 50 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema33/FavoritesContract.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema33; 2 | 3 | // API 4 | public interface FavoritesContract { 5 | 6 | public void putFavorite(Class type, T instance); 7 | public T getFavorite(Class type); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema33/PrintAnnotation.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema33; 2 | 3 | import java.lang.annotation.*; 4 | import java.lang.reflect.*; 5 | 6 | // Use of asSubclass to safely cast to a bounded type token (Page 155) 7 | public class PrintAnnotation { 8 | 9 | static Annotation getAnnotation(AnnotatedElement element, String annotationTypeName) 10 | { 11 | Class annotationType = null; // Unbounded type token 12 | try 13 | { 14 | annotationType = Class.forName(annotationTypeName); 15 | } 16 | catch (Exception ex) 17 | { 18 | throw new IllegalArgumentException(ex); 19 | } 20 | 21 | return element.getAnnotation(annotationType.asSubclass(Annotation.class)); 22 | } 23 | 24 | // Test program to print named annotation of named class 25 | public static void main(String[] args) throws Exception 26 | { 27 | if (args.length != 2) 28 | { 29 | System.out.println("Usage: java PrintAnnotation "); 30 | System.exit(1); 31 | } 32 | 33 | String className = args[0]; 34 | String annotationTypeName = args[1]; 35 | Class klass = Class.forName(className); 36 | System.out.println(getAnnotation(klass, annotationTypeName)); 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema33/TestDrive.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema33; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | public class TestDrive { 7 | 8 | public static void main(String[] args) { 9 | 10 | 11 | // Kısa özetler 12 | // typesafe: String sorduğunda integer beklemeyezsin 13 | // heterogenous: Keylerinin hepsi farklı tipte olabilir. 14 | 15 | // map örneği 16 | HashMap map = new HashMap<>(); 17 | map.put("Furkan", 6); 18 | 19 | 20 | 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema34/Inverse.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema34; 2 | 3 | //Switch on an enum to simulate a missing method (Page 167) 4 | public class Inverse { 5 | public static Operation inverse(Operation op) { 6 | 7 | switch (op) { 8 | case PLUS: 9 | return Operation.MINUS; 10 | case MINUS: 11 | return Operation.PLUS; 12 | case TIMES: 13 | return Operation.DIVIDE; 14 | case DIVIDE: 15 | return Operation.TIMES; 16 | 17 | default: 18 | throw new AssertionError("Unknown op: " + op); 19 | } 20 | } 21 | 22 | public static void main(String[] args) { 23 | double x = 5; 24 | double y = 4; 25 | for (Operation op : Operation.values()) { 26 | Operation invOp = inverse(op); 27 | System.out.printf("%f %s %f %s %f = %f%n", x, op, y, invOp, y, invOp.apply(op.apply(x, y), y)); 28 | } 29 | } 30 | 31 | // Derleme anında değerlerini bildiğiniz bir grup değişmeze ne zaman ihtiyaç duyarsanız enum kullanın 32 | /* 33 | * Her enum sabiti için farklı davranışlar belirlemeniz gereken durumlarda switch deyimi ile farklı davranışları kodlamak yerine 34 | * enum içinde soyut bir metot tanımlayıp her enum sabiti için bu metodu geçersiz kılın. 35 | * Eğer bazı enum sabitlerinin davranışları birbiriyle aynı ise strateji enum desenini uygulayın. 36 | */ 37 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema34/OperationSwitch.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema34; 2 | 3 | public enum OperationSwitch { 4 | 5 | PLUS, MINUS, TIMES, DIVIDE; 6 | 7 | public double apply(double x, double y) { 8 | switch(this) { 9 | case PLUS: return x + y; 10 | case MINUS: return x - y; 11 | case TIMES: return x * y; 12 | case DIVIDE: return x / y; 13 | } 14 | throw new AssertionError("Unknown op: " + this); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema34/PayrollDay.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema34; 2 | 3 | import static com.effectivejava.tutorial.effectivejava.itema34.PayrollDay.PayType.*; 4 | 5 | //The strategy enum pattern (Page 166) 6 | enum PayrollDay { 7 | MONDAY(WEEKDAY), TUESDAY(WEEKDAY), WEDNESDAY(WEEKDAY), THURSDAY(WEEKDAY), FRIDAY(WEEKDAY), SATURDAY(WEEKEND), 8 | SUNDAY(WEEKEND); 9 | 10 | private final PayType payType; 11 | 12 | // Constructor 13 | PayrollDay(PayType payType) { 14 | this.payType = payType; 15 | } 16 | 17 | int pay(int minutesWorked, int payRate) { 18 | return payType.pay(minutesWorked, payRate); 19 | } 20 | 21 | // The strategy enum type 22 | enum PayType { 23 | WEEKDAY { 24 | int overtimePay(int minsWorked, int payRate) { 25 | return minsWorked <= MINS_PER_SHIFT ? 0 : (minsWorked - MINS_PER_SHIFT) * payRate / 2; 26 | } 27 | }, 28 | WEEKEND { 29 | int overtimePay(int minsWorked, int payRate) { 30 | return minsWorked * payRate / 2; 31 | } 32 | }; 33 | 34 | abstract int overtimePay(int mins, int payRate); 35 | 36 | private static final int MINS_PER_SHIFT = 8 * 60; 37 | 38 | int pay(int minsWorked, int payRate) { 39 | int basePay = minsWorked * payRate; 40 | return basePay + overtimePay(minsWorked, payRate); 41 | } 42 | } 43 | 44 | public static void main(String[] args) { 45 | for (PayrollDay day : values()) 46 | System.out.printf("%-10s%d%n", day, day.pay(8 * 60, 1)); 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema34/Planet.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema34; 2 | 3 | //Enum type with data and behavior (159-160) 4 | public enum Planet { 5 | 6 | MERCURY(3.302e+23, 2.439e6), VENUS(4.869e+24, 6.052e6), EARTH(5.975e+24, 6.378e6), MARS(6.419e+23, 3.393e6), 7 | JUPITER(1.899e+27, 7.149e7), SATURN(5.685e+26, 6.027e7), URANUS(8.683e+25, 2.556e7), NEPTUNE(1.024e+26, 2.477e7); 8 | 9 | private final double mass; // In kilograms 10 | private final double radius; // In meters 11 | private final double surfaceGravity; // In m / s^2 12 | 13 | // Universal gravitational constant in m^3 / kg s^2 14 | private static final double G = 6.67300E-11; 15 | 16 | // Constructor 17 | Planet(double mass, double radius) { 18 | this.mass = mass; 19 | this.radius = radius; 20 | surfaceGravity = G * mass / (radius * radius); 21 | } 22 | 23 | public double mass() { 24 | return mass; 25 | } 26 | 27 | public double radius() { 28 | return radius; 29 | } 30 | 31 | public double surfaceGravity() { 32 | return surfaceGravity; 33 | } 34 | 35 | public double surfaceWeight(double mass) { 36 | return mass * surfaceGravity; // F = ma 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema34/TestDrive.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema34; 2 | 3 | import java.math.RoundingMode; 4 | 5 | public class TestDrive { 6 | 7 | // Tür güvenliği yok. 8 | // Debug zorlaşacak 9 | 10 | public static final int APPLE_FUJI = 0; 11 | public static final int APPLE_PIPPIN = 1; 12 | public static final int APPLE_GRANNY_SMITH = 2; 13 | 14 | public static final int ORANGE_NAVEL = 0; 15 | public static final int ORANGE_TEMPLE = 1; 16 | public static final int ORANGE_BLOOD = 2; 17 | 18 | 19 | public enum Apple { FUJI, PIPPIN, GRANNY_SMITH } 20 | public enum Orange { NAVEL, TEMPLE, BLOOD } 21 | 22 | // Singletonların genelleştirilmiş hali 23 | 24 | /* 25 | * Tanımlı her enum sabitini public static final bir alan olarak dışa açan sınıflar olarak düşünülebilirler. 26 | * Enum türleri kalıtılamazlar ve dışarıdan erişilebilir yapıcı metotları olmadığından başka kod parçaları tarafından yaratılamazlar. 27 | */ 28 | 29 | public static void main(String[] args) { 30 | 31 | //RoundingMode 32 | 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema34/WeightTable.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema34; 2 | 3 | //Takes earth-weight and prints table of weights on all planets (Page 160) 4 | public class WeightTable { 5 | 6 | public static void main(String[] args) { 7 | double earthWeight = 10; 8 | double mass = earthWeight / Planet.EARTH.surfaceGravity(); 9 | for (Planet p : Planet.values()) 10 | System.out.printf("Weight on %s is %f%n", p, p.surfaceWeight(mass)); 11 | } 12 | 13 | 14 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema35/Ensemble.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema35; 2 | 3 | //Enum with integer data stored in an instance field (Page 168) 4 | public enum Ensemble { 5 | SOLO(1), DUET(2), TRIO(3), QUARTET(4), QUINTET(5), SEXTET(6), SEPTET(7), OCTET(8), DOUBLE_QUARTET(8), NONET(9), 6 | DECTET(10), TRIPLE_QUARTET(12); 7 | 8 | private final int numberOfMusicians; 9 | // CONSTRUTOR 10 | Ensemble(int size) { 11 | this.numberOfMusicians = size; 12 | } 13 | 14 | public int numberOfMusicians() { 15 | return numberOfMusicians; 16 | } 17 | 18 | public static void main(String[] args) { 19 | 20 | System.out.println(QUARTET.numberOfMusicians); 21 | 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema35/OrdinalExample.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema35; 2 | 3 | public enum OrdinalExample { 4 | 5 | RED,BLACK,BLUE; 6 | 7 | 8 | public static void main(String[] args) { 9 | 10 | System.out.println("Red Ordinal: " + RED.ordinal()); 11 | System.out.println("Red Ordinal: " + BLACK.ordinal()); 12 | System.out.println("Red Ordinal: " + BLUE.ordinal()); 13 | 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema35/TestDrive.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema35; 2 | 3 | public class TestDrive { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema36/TestDrive.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema36; 2 | 3 | public class TestDrive { 4 | 5 | public static void main(String[] args) { 6 | 7 | // 0000 8 | // 0001 -> 1 9 | // 0010 -> 2 10 | // 0011 -> 3 11 | 12 | 13 | int x = -4; 14 | System.out.println(x>>1); 15 | 16 | int y = 4; 17 | // 0100 -> 0010 18 | System.out.print("Şu an bu örnekteyiz "); 19 | System.out.println(y>>1); 20 | 21 | int xNeg = -1; 22 | // 11111....1 -> 0000000.....000001 23 | System.out.println(x>>>30); // 000000...0011 24 | 25 | 26 | int diffY = 2; 27 | // 0010 28 | System.out.println(diffY<<1); 29 | 30 | 31 | // 0001 -> 0100 32 | System.out.println(1<<2); 33 | 34 | 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/effectivejava/tutorial/effectivejava/itema36/Text.java: -------------------------------------------------------------------------------- 1 | package com.effectivejava.tutorial.effectivejava.itema36; 2 | 3 | import java.util.*; 4 | 5 | //EnumSet - a modern replacement for bit fields (Page 170) 6 | public class Text { 7 | 8 | public enum Style { 9 | BOLD, ITALIC, UNDERLINE, STRIKETHROUGH 10 | } 11 | 12 | // Any Set could be passed in, but EnumSet is clearly best 13 | public void applyStyles(Set