Apeiron Kambyses
Apeiron Kambyses

Reputation: 99

getFont() method

I just wanted to ask a question about the getFont() method, which is in java.awt.Font. I don't understand why the getStyle() is undefined for type string, although it actually should work with strings. In API it says it takes an integer as argument.

    import java.awt.Font;
//import java.util.*;
public class NotEqual 

{
    public static void main(String[] args)
    {
    //Scanner input = new Scanner(System.in);
    //System.out.println("Write something ");
    String sentence  = "The sentence";
    //int x = 2;
    //System.out.println(sentence.getFont());
    //System.out.println(sentence.getSize());
    //System.out.println(sentence.getStyle());
    System.out.println(sentence.getFont());

    }
}

Upvotes: 0

Views: 5123

Answers (3)

Andrew Thompson
Andrew Thompson

Reputation: 168825

One reason might be that the int return value is easier to interpret than either BOLD+ITALIC or ITALIC+BOLD (same style, same int, different String).


Noting also that arguments can be overloaded but return types cannot, it could be argued that the int is the better value to return.

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

Your code won't work because Strings don't have fonts. Period. All they are are lists of chars with supporting methods and properties, but no font. To see what methods you can call on String, look at the API as that is the final arbitrator of what you can and cannot do with them. In fact, if you search the text of the String API, you won't even find the word "font" present anywhere.

I still don't understand the part about it "In API it says it takes an integer as argument" though.

Upvotes: 2

OverZealous
OverZealous

Reputation: 39570

Style is a integer, defined by the constants Font.PLAIN, Font.BOLD, or Font.ITALIC.

From the docs:

Returns the style of this Font. The style can be PLAIN, BOLD, ITALIC, or BOLD+ITALIC.

It is never a string. A string is not one of the accepted values. (It never has been.)

Upvotes: 3

Related Questions