✨
Java
  • 자바의 역사
  • Ch1
    • Before Start Java
      • History of Java
      • Feature of Java
      • API Documentation
      • Hello World!
      • eclipse shortcut
      • Eclipse IDE
      • GitHub to eclipse
  • Ch2
    • Variable
      • Variable
    • Variable Type
    • Character and String
    • Primitive type and range
    • printf and formatter
    • Scanner
    • Overflow of Int
    • Transition between different variable types
    • Object Array
  • CH3
    • Operator
  • CH4
    • 조건문과 반복문
    • if statement
    • switch
    • Math.random()
    • for statement
    • while statement
    • break, continue, named iterated statement
  • CH5
    • Array
    • 배열 활용
    • String Array
  • OOP
    • Intro
  • Class and Object
  • Make multiple Classes in one file
  • How to use Object
  • Object Array
  • Variable type according to declared location
  • Class Variable and Instance Variable
  • Method
  • Call Stack
  • Parameter
  • Static method(Class method) and Instance method
  • (Method)Overloading
  • Constructor
  • Constructor this()
  • Reference type variable "this"
  • Initialize variable
  • Inheritance
  • Composite
  • Single Inheritance, Object Class
  • (Method)Overriding
  • super, super()
  • package, class path
  • import, static import
  • modifier
  • access modifier
  • Encapsulation
  • Polymorphism
  • reference type transition
  • instanceof operator
  • Parameter polymorphism
  • Multiple object using one array
  • Abstract Class, Abstract Method
  • Creating Abstract Class
  • Interface
  • Interface Polymorphism
  • Interface Pros
  • Default method and static method
  • Inner Class
  • Anonymous Class
  • java.lang package and useful class
    • Object class
    • hashCode(), toString()
    • String class
    • StringBuffer class
    • StringBuilder class
    • Math class
    • Wrapper class
    • Number class
    • String to Number, String to Wrapper class
    • Auto-boxing and (auto)Unboxing
  • Collection Framework
    • Collections framework
    • List, Set, Map Interface
    • List의 removeAll() 과 clear() 비교
    • List Interface - ArrayList
    • How to view Java API source
    • List Interface - LinkedList
    • Stack and Queue
    • Iterator, ListIterator, Enumeration
    • Array
    • Comparator와 Comparable
    • Stack
    • String
    • String + char = String
    • String.toCharArray()
    • BufferedReader / BufferWriter
    • Scanner로 String 입력 - next( )와 nextLine( )
    • 공백없이 2차원배열 데이터 String으로 한번에 한줄씩 입력받기(문자/숫자)
    • 공백을 사이에 두고 빠른 2차원 배열 입출력
    • arr[index++]과 arr[index] index++의 차이
    • int와 long의 차이
    • Untitled
    • 타입 간의 변환
    • Array 와 ArrayList
    • valueOf()
    • Char
    • 변수, 객체 초기화(초기값 설정)
  • error troubleshooting
    • No enclosing instance of type
    • ASCII Code Table
    • java.lang.NumberFormatException
    • No enclosing instance..
  • reference
    • String을 생성하는 2가지 방법과 차이점
    • StackOverflowError(스택, 힙 메모리)
    • swtich-case 반복문
Powered by GitBook
On this page

Was this helpful?

  1. Ch2

printf and formatter

printf와 지시자(%d, %f, %s,...)

  • println()의 단점 : 출력형식 지정불가

  1. 실수의 자리수 조절 불가(소수점 n자리까지 출력할 수 없다.)

System.out.prinln(10.0/3);//3.333333...

2. 10진수로만 출력한다.

System.out.prinln(0x1A);//26
  • printf()로 출력형식 지정가능 : 지시자(%f, %d, %X,...)

  1. 소수점 둘째자리까지 출력

System.out.prinf("%.2f",10.0/3);//3.33

2. 10진수로 출력

System.out.prinf("%d",0x1A);//26

3. 16진수로 출력

System.out.prinf("%X",0x1A);//1A
  • Useful Formatter of printf() : 자주 쓰이는 printf()의 지시자

Formatter

Description

%b

boolean 형식으로 출력

%d

decimal(10진) 형식으로 출력

%o

octal(8진) 형식으로 출력

%x, %X

hexa-decimal(16진) 형식으로 출력

%f

floating-point(부동소수점;떠다니는) 형식으로 출력

%e, %E

exponent(지수) 형식으로 출력

%c

character(문자) 형식으로 출력

%s

string(문자열) 형식으로 출력

//8진수, 16진수에 접두사가 붙이기
System.out.prinf("%#o",15);//017
System.out.prinf("%#x",15);//0xf
System.out.prinf("%#X",15);//0Xf

실수 출력을 위한 지시자 %f - 지수형식(%e), 간략한 형식(%g)

실수를 표현할 땐 %f를 쓰고, 0이 많이 들어간다면 %e(지수형식)을 많이 쓴다.

float f = 123.4567890f;

//float은 정밀도가 7자리이므로 소수점을 포함한 7자리까지만 의미있고 그 이하로는 부정확한 값이다.
System.out.printf("%f", f);//123.456787. 소수점아래 6자리까지 보여준다.
System.out.printf("%e", f);//1.234568e+02. '8'은 반올림 되었고, e+02 = 10^2이다.

간략한 형식(%g)

System.out.prinf("%g",123.456789);//123.457 소수점을 포함하여 7자리로 보여준다.%f처럼
System.out.prinf("%g",0.0000001);//1.00000e-8 지수로 표현하는 게 더 간략해서 %e처럼 지수로 표현.

더 많은 formatter를 확인하려면 Java API 문서에서 Formatter를 검색해서 참고하면 된다.

prinln은 자동 줄바꿈이 되지만, printf는 %n이나 \n 같은 개행문자를 넣어줘야한다. %n은 OS에 상관없이 적용되므로 %n을 사용하도록 하는 것이 좋다.

//정수 
System.out.printf("[%5d]%n",10);//_ _ _ 1 0;오른쪽 정렬 
System.out.printf("[%-5d]%n",10);//1 0 _ _ ;왼쪽 정렬 
System.out.printf("[%05d]%n",10);//00010출력 
System.out.printf("[%5d]%n",1234567);//1234567출력. 5자리로 지정했지만 넘기 때문에 그냥 다 출력된다.
		
//실수 
double d = 1.23456789;
System.out.printf("%f%n",d);//1.234568
System.out.printf("%14.10f%n",d);//소수점포함 전체 14자리, 소수점 아래 10자리.정수부분은 공백문자, 소수점 부분은 0으로 채워진다.
System.out.printf("%.6f%n",d);//소수점 아래 6자리까지표현 
		
//문자열 
System.out.printf("[%s]%n","www.codechobo.com");
System.out.printf("[%20s]%n","www.codechobo.com");//20자리 지정 
System.out.printf("[%-20s]%n","www.codechobo.com");//20자리 지정, 왼쪽 정렬 
System.out.printf("[%.10s]%n","www.codechobo.com");//10자리까지만 잘라서 출력 가능

PreviousPrimitive type and rangeNextScanner

Last updated 4 years ago

Was this helpful?